Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Compares two arrays and shows exact difference between them.
  5. *
  6. * !!! IMPORTANT !!
  7. * Order of two supplied arrays matters!
  8. *
  9. * @fix xZero707 <xzero@elite7hackers.net>: Pay attention to types as well
  10. *
  11. * [NOTE BY danbrown AT php DOT net: The array_diff_assoc_recursive function is a
  12. * combination of efforts from previous notes deleted.
  13. * Contributors included (Michael Johnson), (jochem AT iamjochem DAWT com),
  14. * (sc1n AT yahoo DOT com), and (anders DOT carlsson AT mds DOT mdh DOT se).]
  15. *
  16. * @param array $array1
  17. * @param array $array2
  18. * @return array|int
  19. */
  20. function array_diff_assoc_recursive(array $array1, array $array2)
  21. {
  22. foreach ($array1 as $key => $value) {
  23. if (is_array($value)) {
  24. if (!array_key_exists($key, $array2)) {
  25. $difference[$key] = $value;
  26. } elseif (!is_array($array2[$key])) {
  27. $difference[$key] = $value;
  28. } else {
  29. $new_diff = $this->array_diff_assoc_recursive($value, $array2[$key]);
  30. if ($new_diff != FALSE) {
  31. $difference[$key] = $new_diff;
  32. }
  33. }
  34. } elseif (!array_key_exists($key, $array2) || $array2[$key] != $value) {
  35. $difference[$key] = $value;
  36. }
  37. }
  38. return (!isset($difference) ? 0 : $difference);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement