Comparing multidimentional json object or 2d array in PHP

Most of the time we stuck in the situations where we need to compare 2d arrays and find difference. Here I am providing an easy solution. In which we doesn’t need any foreach loop 2 times for doing this.

 

print_r(
        array_map("unserialize",
                  array_diff_assoc(
                                   array_map("serialize", json_decode($array1)),
                                   array_map("serialize", json_decode($array2))
                                   )
                  )
       );

So its really easy and it just returns array where it finds any difference.

How It works?

If you look closer “What we are doing” it’s just a small trick.
1)Serializing the 2-d ARRAY so that we can find diference by using PHP method “array_diff_assoc”
2)Using array_map to walk through each element
3)Unserialize the resulted array elements