PHP has functions that can compute the difference between two arrays built in. The comments sections for those functions are filled with people trying to figure out the best way to do the same thing with multidimensional arrays, and almost all of them are recursive diffing functions that try to walk the tree and do a diff at each level.
The problem with this approach are 1) they are unreliable as they usually don’t account for all data types at each level, and 2) they’re slow, due to multiple calls to array_diff at each level of the tree.
A better approach, I think, is to flatten a multidimensional array into a single dimension, make a single call to array_diff, then (if needed) expand it back out if you really need the resulting diff to be multidimensional.
Lets look at some code. The following recursive function flattens a multidimensional array into a single dimension.
The following function (based on this function found here) reinflates the array back up after it’s been deflated.
Now, with arrays in flat form, it’s easy to use the built-in functions to diff: