1

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, 

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

6 Comments

Omg.. Nice ideas,... Thanks... :D
I count four array methods used, one in a nested foreach(), of which there are two. This code is wandering around trying to accomplish a reductive outcome. Why not just use array_reduce()?
You might have missed my point. Take a look at the answer I provided; the procedure in this answer is quite a bit more inefficient than just reducing the source array into a collection based on a check for presence.
@JaredFarrish got it @lyndra you can array_reduce as well see JaredFarrish's anwser
@JaredFarrish I dont understand :( can you give example?
|
1

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);

Comments

1

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

https://3v4l.org/Xeaek

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);

2 Comments

I change $data to url.. Get null... :?
@lyndria I reincorporated an example into the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.