This question already has an answer here:
I have 2 object arrays: Array A and Array B. how can I check if object from Array B exists in Array A. and if exists remove it from Array A.
Example:
Array A:
[
{"id": 1, "name": "item1"},
{"id": 2, "name": "item2"},
{"id": 3, "name": "item3"},
{"id": 4, "name": "item4"}
]
Array B
[
{"id": 1, "name": "item1"},
{"id": 3, "name": "item3"}
]
After removing Array A should look like:
[
{"id": 2, "name": "item2"},
{"id": 4, "name": "item4"}
]
You can use array_udiff, and you can refer to these post for array compare post1 and post2. live demo
array_filter
might help.http://php.net/manual/en/function.array-filter.php
http://php.net/manual/ru/function.in-array.php
Here we are using
array_map
which first convertobject
's to string usingjson_encode
which will convert array tojson string
then we are findingarray_diff
for both the array's.Try this code snippet here