check difference between multidimensional arrays

为君一笑 提交于 2020-03-06 02:16:22

问题


i'm trying a way to remove from a multidimensional array, all the elements equal to another multidimensional array.

For example, i've these two arrays;

$array1 = Array ( [0] => Array ( [item1] => 3017, [item2] => 7 ), [1] => Array ( [item1] => 3018, [item2] => 4 ), [2] => Array ( [item1] => 3020, [item2] => 9 ), [3] => Array ( [item1] => 3024, [item2] => 5 ) ) 

and

$array2 = Array ( [0] => Array ( [item1] => 3017, [item2] => 7 ), [1] => Array ( [item1] => 3018, [item2] => 200 ), [2] => Array ( [item1] => 3020, [item2] => 300 ), [3] => Array ( [item1] => 3024, [item2] => 5 ) ) 

The difference beetween these two arrays is the value of [item2] in element [1] and [2].

I want get an array that contains only the different values of the first array. In my case, should be:

array_diff = array( [1] => Array ( [item1] => 3018, [item2] => 4 ), [2] => Array ( [item1] => 3020, [item2] => 9 ) )

回答1:


Use json comparison ;)

$jsonDiff = array_diff(array_map('json_encode', $array1), array_map('json_encode', $array2));
$arrayDiff = array_map('json_decode', $diff);

regards.



来源:https://stackoverflow.com/questions/44784255/check-difference-between-multidimensional-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!