Reputation: 53
How can I use PHP to remove a duplicate from JSON?
<?php
if(!function_exists("curl_init")) die("cURL extension is not installed");
$json = 'https://sug.daraz.pk/sug?area=lazada_pk_web&code=utf-8&q=book';
$ch=curl_init($json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$r=curl_exec($ch);
curl_close($ch);
$arr = json_decode($r,true);
foreach($arr['result'] as $item) {
foreach ($item["model"] as $key => $value) {
$pattern = '/\s+/';
$string = $value.' ';
$split = preg_split( $pattern, $string);
$go = implode(', ', $split);
echo $go;
}
}
?>
I get result :
books, book, shelf, book, rack, bookmarks, books, for, kids, book, holder, book, shelve, book, stand, book, end, book, reader,
I want the result to be in text like this:
books, book, shelf, rack, bookmarks, for, kids, holder, shelve, stand, end, reader,
Upvotes: 1
Views: 199
Reputation: 49198
This is a fairly standard reduce function. Note the array passed in the very end, and that I explode the search terms.
$data = json_decode('{"result":[{"type":"nt-common","model":{"query":"books"}},{"type":"nt-common","model":{"query":"book shelf"}},{"type":"nt-common","model":{"query":"book rack"}},{"type":"nt-common","model":{"query":"bookmarks"}},{"type":"nt-common","model":{"query":"books for kids"}},{"type":"nt-common","model":{"query":"book holder"}},{"type":"nt-common","model":{"query":"book shelve"}},{"type":"nt-common","model":{"query":"book stand"}},{"type":"nt-common","model":{"query":"book end"}},{"type":"nt-common","model":{"query":"book reader"}}]}');
$queries = array_reduce($data->result, static function($found, $result) {
$items = explode(' ', $result->model->query);
foreach($items as $item) {
if (!in_array($item, $found)) {
$found[] = $item;
}
}
return $found;
}, []);
sort($queries);
Gives:
book bookmarks books end for holder kids rack reader shelf shelve stand
Putting it together:
$fetch = static function(string $uri) {
function_exists('curl_init') || die('cURL extension is not installed.');
$handle = curl_init($uri);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$data = json_decode(curl_exec($handle));
curl_close($handle);
return $data;
};
$modelQueries = static function($found, $result) {
$items = explode(' ', $result->model->query);
foreach($items as $item) {
if (!in_array($item, $found)) {
$found[] = $item;
}
}
return $found;
};
$bookModels = 'https://sug.daraz.pk/sug?area=lazada_pk_web&code=utf-8&q=book';
$books = $fetch($bookModels);
$bookQueries = array_reduce($books->result, $modelQueries, []);
sort($bookQueries);
Upvotes: 1
Reputation: 2321
I am using array_unique
and array_merge
if(!function_exists("curl_init")) die("cURL extension is not installed");
$json = 'https://sug.daraz.pk/sug?area=lazada_pk_web&code=utf-8&q=book';
$ch=curl_init($json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$r=curl_exec($ch);
curl_close($ch);
$arr = json_decode($r,true);
$result = array();
foreach($arr['result'] as $item) {
foreach ($item["model"] as $key => $value) {
$pattern = '/\s+/';
$string = $value.' ';
$split = preg_split( $pattern, $string);
$result = array_merge($result, $split);
}
}
$result = array_values(array_filter(array_unique($result), function($value) { return !is_null($value) && $value !== ''; }));
print implode(', ', $result);
output as expected:
books, book, shelf, rack, bookmarks, for, kids, holder, shelve, stand, end, reader
Upvotes: 2
Reputation: 484
The best practice for your needs is to create a new Array
and push your keys into it using a condition to check if they aren't already into the array using the in_array
function.
For example:
<?php
if(!function_exists("curl_init")) die("cURL extension is not installed");
$json = 'https://sug.daraz.pk/sug?area=lazada_pk_web&code=utf-8&q=book';
$ch=curl_init($json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$r=curl_exec($ch);
curl_close($ch);
$arr = json_decode($r, true);
$data = array();
foreach($arr['result'] as $item) {
foreach ($item["model"] as $key => $value) {
$pattern = '/\s+/';
$string = $value.' ';
$split = preg_split( $pattern, $string);
$go = implode(', ', $split);
if (!in_array($go, $data)) {
array_push($data, $go);
}
}
}
echo implode(',', $data);
Upvotes: 1