0

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";
    }
  }
}
11
  • Can you give us the sample you're using for $array1 and $array2 Commented Jun 14, 2010 at 22:02
  • 2
    Maybe you misspoke - if both value1 and value2 are arrays the first if statement evaluates to true and the elseifs will be ignored... The first elseif should trigger if one value is an array and the other is not, right? Also, (I think) is_array will return true/false - so it's good practice to use "!==" instead of "!=". Commented Jun 14, 2010 at 22:03
  • yes, I did mistype. I have corrected the example in the original question. The problem occurs in the second level of recursion comparing the first arrays ie item1[0][n] vs item2[0][n] where the value of both are now integers. Commented Jun 14, 2010 at 22:22
  • Josh - I get to the line with the original "if" then it jumps back to the top of the loop and compares the next items on the list. Commented Jun 14, 2010 at 22:24
  • thetaiko - The arrays that I am using are very large. They are the jpeg headers from image files and have at least 4 dimensions and many elements within the dimensions. If I use print_r() to print the array it extends past the 32K length limit. Commented Jun 14, 2010 at 22:32

4 Answers 4

2

could it be because you have

else;

at the end...?

Try removing that or turning that into 'real code'

Sign up to request clarification or add additional context in comments.

1 Comment

I removed the else; line. The function still behaves the same way. The problem occurs after it has launched the recursive function to start the comparison of the first arrays ie item1[0][n] vs item2[0][n] where the value of both are now integers.
0

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...?

4 Comments

Adding the default value was a good suggestion. When I step through my code the $array1 variable continues to show the ORIGINAL array rather than the second level. Do I need to do something different in php to pass the array by value?
Huh? You mean $level shows the next level, but $array1 stays the same? That shouldn't happen. Is the code you posted above EXACTLY what you have? If so, try disabling your debugger... If not, update with the exact code (Because what you posted actually works fine)...
The debugger was behaving oddly. I shutdown and restarted eclipse. Now it appears that my code is working properly, but there is no difference between the arrays I was comparing!
I assumed that the metadata would be different between the 2 files, but apparently that is not the case. In any case, we now have a useful function to quickly point out the difference between 2 multidimensional arrays. Thanks for your help and the other expert's as well that pointed out formatting change suggestions and the use of a default value
0

This doesn't exactly answer your question, but I think Adobe Bridge saves metadata in dotfiles in the same directory as the files. For example, sort information is saved in a .bridgesort file.

1 Comment

Thanks for the tip. Somehow the ratings I created in bridge are viewable in VISTA even though I don't copy the dot files to the new directory when I copy the jpg.
0

The only way that all the elseifs would be skipped is if the two variables are not arrays and are equal.

6 Comments

I find it odd that even the first elseif isn't reached. Is there something due to recursion is causing the odd behavior? When I highlight the statement inside the first if statement it shows that the result is false (neither is an array), but the subsequent elseif is not reached.
But if neither of them is an array then the second elseif is false.
Did you perhaps mean the && to be || instead?
Ignacio - I mistyped my example. It has been corrected in the original question
The if does not check if either of them is an array, it checks if both of them are arrays. Your description does not match your code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.