Posted By

zoranmk on 10/27/10


Tagged

array compare associative


Versions (?)

Compare two associative arrays


 / Published in: PHP
 

  1. function compareAssociativeArrays($first_array, $second_array)
  2. {
  3. $result = array_diff_assoc($first_array, $second_array);
  4. if(!empty($result))
  5. {
  6. //Arrays are different
  7. //print_r($result); will return the difference as key => value pairs.
  8. return FALSE;
  9. }
  10. else
  11. {
  12. //Arrays are same.
  13. //print_r($result); returns empty array.
  14. return TRUE;
  15. }
  16. }
  17.  
  18. //Usage:
  19. $first = array(
  20. 'name' => 'Zoran',
  21. 'smart' => 'not really'
  22. );
  23.  
  24. $second = array(
  25. 'smart' => 'not really',
  26. 'name' => 'Zoran'
  27. );
  28.  
  29. if(compareAssociativeArrays($first, $second))
  30. {
  31. echo 'Arrays are same';
  32. }
  33. else
  34. {
  35. echo 'Arrays are different';
  36. }

Report this snippet  

You need to login to post a comment.