2

I am trying to use array_diff in order to see what changes had been made to an account. The account data is encoded as json files. The problem is that when I decode the json, I get a multidimensional array. As specified in the manual:

Note:  Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]); 

This comment seems to solve the problem- but what if I do not know how many dimensions are there, neither the depth of the arrays? To give a really simple example, I have those two files: abcd.json:

[{"name":"Tim Pearson","id":"17118"},{"name":"Ashley Danchen Chen","id":"504829084"},{"name":"Foisor Veronica","id":"100005485446135"}]

And blabla.json:

[{"name":"Tim Pearson","id":"17118"},{"name":"Foisor Veronica","id":"100005485446135"}]

And if I decode each of them, they will look like:

Array ( [0] => Array ( [name] => Tim Pearson [id] => 17118 ) [1] => Array ( [name] => Ashley Danchen Chen [id] => 504829084 ) [2] => Array ( [name] => Foisor Veronica [id] => 100005485446135 ) )

So what would I need is to make the difference, it would remain {"name":"Ashley Danchen Chen","id":"504829084"}. But how to do that? array_diff seems to be difficult to use now.

ScottMcGready
  • 1,612
  • 2
  • 24
  • 33
Cornelia Secelean
  • 403
  • 1
  • 4
  • 14
  • Just try the [example](http://php.net/manual/en/function.array-diff.php#37527). It might sound more complicated than it really is ;-) What it does is, it uses [array_walk](http://php.net/manual/en/function.array-walk.php) to [serialize](http://php.net/manual/en/function.serialize.php) the sub arrays. Then compare the contents and [deserialize](http://www.php.net/manual/en/function.unserialize.php) the result of the diff. – Havelock May 09 '13 at 14:37
  • @Havelock As I asked, what if I don't know how many sub-arrays are there, neither the depth of each? – Cornelia Secelean May 09 '13 at 14:52
  • The `serialize` function will manage that. – Havelock May 09 '13 at 14:59

0 Answers0