Guest User

Untitled

a guest
Jun 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. <?php
  2.  
  3. function is_deeply($a, $b, $diff = array(), $key_str = '') {
  4. ksort($a);
  5. ksort($b);
  6.  
  7. $keys = array_unique(
  8. array_merge(
  9. array_keys($a),
  10. array_keys($b)
  11. )
  12. );
  13.  
  14. foreach($keys as $key) {
  15. $index = $key_str . '['. $key . ']';
  16. // aがない
  17. if(! isset($a[$key]) && ! is_null($a[$key])) {
  18. $diff[$index] = array(
  19. "undefined",
  20. "$b[$key] (" . gettype($b[$key]) . ')',
  21. );
  22. continue;
  23. }
  24. // bがない
  25. else if(! isset($b[$key]) && ! is_null($b[$key])) {
  26. $diff[$index] = array(
  27. "$a[$key] (" . gettype($a[$key]) . ')',
  28. "undefined",
  29. );
  30. continue;
  31. }
  32. // 両方ある
  33. else {
  34. // 両方配列
  35. if(is_array($a[$key]) && is_array($b[$key])) {
  36. $diff = is_deeply($a[$key], $b[$key], $diff, $index);
  37. continue;
  38. }
  39. // 一致しない
  40. if($a[$key] !== $b[$key]) {
  41. $diff[$index] = array(
  42. "$a[$key] (" . gettype($a[$key]) . ')',
  43. "$b[$key] (" . gettype($b[$key]) . ')',
  44. );
  45. continue;
  46. }
  47. // 一致する
  48. else {
  49. continue;
  50. }
  51. }
  52. }
  53.  
  54. return $diff;
  55. }
Add Comment
Please, Sign In to add comment