0

I was following this but it still doesn't work for me: How to remove duplicate data of JSON object using PHP

In my PHP file I have 3 JSON arrays:

{"results":[{"cat_id":2,"cat_name":"veterinarian"},{"cat_id":3,"cat_name":"electrician"},
{"cat_id":4,"cat_name":"dentist"}]}

Array2:

 {"results":[{"cat_id":"8","cat_name":"dental hygienist"},{"cat_id":"5","cat_name":"stocktaker"},
{"cat_id":"9","cat_name":"builder"}]}

Array3:

{"results":[{"cat_id":4,"cat_name":"dentist"},{"cat_id":5,"cat_name":"stocktaker"},
{"cat_id":3,"cat_name":"electrician"}]}

I want to merge these and remove duplicates. I'm trying like this:

//this works good, all merged into one
$array1AndArray2AndArray3 = array_merge_recursive($array1,$array2, $array3);

//now I want to remove duplicates:
$uniqueArray = array_values(array_unique($array1AndArray2AndArray3, SORT_REGULAR));

echo "Unique array is " . json_encode($uniqueArray);

But I am getting:

Unique array is [[{"cat_id":2,"cat_name":"veterinarian"},{"cat_id":3,"cat_name":"electrician"},
{"cat_id":4,"cat_name":"dentist"},
{"cat_id":"8","cat_name":"dental hygienist"},{"cat_id":"5","cat_name":"stocktaker"},
{"cat_id":"9","cat_name":"builder"},
{"cat_id":4,"cat_name":"dentist"},
{"cat_id":5,"cat_name":"stocktaker"},
{"cat_id":3,"cat_name":"electrician"}]]

As you can see, duplicates are not removed, and there's extra [] and "results" is missing.

Can you tell me how I can fix this, or another way to do it?

1 Answer 1

1

Use below solution

$array1 = '{"results":[{"cat_id":2,"cat_name":"veterinarian"},{"cat_id":3,"cat_name":"electrician"},{"cat_id":4,"cat_name":"dentist"}]}';
$array2 = '{"results":[{"cat_id":"8","cat_name":"dental hygienist"},{"cat_id":"5","cat_name":"stocktaker"},{"cat_id":"9","cat_name":"builder"}]}';
$array3 = '{"results":[{"cat_id":4,"cat_name":"dentist"},{"cat_id":5,"cat_name":"stocktaker"},{"cat_id":3,"cat_name":"electrician"}]}';

$array1 =  json_decode($array1, TRUE);
$array2 =  json_decode($array2, TRUE);
$array3 =  json_decode($array3, TRUE);

$array4 = array_merge_recursive($array1['results'], $array2['results'], $array3['results']);

$uniqueArray['results'] = array_values(array_unique($array4, SORT_REGULAR));
Sign up to request clarification or add additional context in comments.

4 Comments

Hehe looks familiar, @CHarris, maybe add into your post that the JSON arrays are converted for display and/or are stored as PHP arrays to begin with
@TCooper - I had updated your code with that but you may be removed that answer :)
yeah, didn't want to clutter the page with useless info (my answer). Sorry to hijack your answer Jaydp - I recently answered similarly
Good stuff, thanks - just made a small edit to get the 'results' part.

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.