I am writing a recursive function to print out the differences between 2 multildimensional php arrays. The purpose of this code is to see the difference between jpeg headers to deteremine how adobe bridge cs3 is saving rating information within the jpg file.
When I am single-stepping through the code using my eclipse - zend debugger ide, it appears that even though the initial if statement is false (ie neither values is an array), the subsequent elseif statements are never executed. The function is attached below.
Note: Changes since original post based on comments
Added a default level= ''
Removed comments between the if{} elseif{} blocks
Removed an else; at the end of the block that had no function
Encoded the < and > symbols so angle bracket would show in my code
function array_diff_multi($array1,$array2,$level=''){ $keys = array_keys($array1); foreach($keys as $key) { $value1 = $array1[$key]; if(array_key_exists($key,$array2) ){ $value2 = $array2[$key]; if (is_array($value1) && is_array($value2)){ // Check if they are both arrays, if so recursion is needed array_diff_multi($value1,$value2,$level . "[ " . $key . " ]"); } elseif(is_array($value1) != is_array($value2)){ // Recursion is not needed, check if comparing an array to another type print "<br>" . $level . $key ."=>" . $value1 . "as array, compared to ". $value2 ."<br>"; } elseif($value1 != $value2){ // the values don't match, print difference print "<br>" . $level . $key ."=>" . $value1 ." != " . $value2 ."<br>"; } } else{ print "<br>" . $level. $key . "does not exist in array2"; } } }
It works fine for me here. I put your function (with the tiny difference of adding a default value of '' to the level parameter), and these two arrays:
$a1 = array('foo', 'bar', 2, array('baz', '3', 4, array(54,45)));
$a2 = array('faz', 'bar', 4, array('buz', '3', 5, 54));
And got this output:
0=>foo != faz
2=>2 != 4
[ 3 ]0=>baz != buz
[ 3 ]2=>4 != 5
[ 3 ]3=>Arrayas array, compared to 54
Perhaps your starting arrays are not what you think they are...?