Skip to content

Instantly share code, notes, and snippets.

@handlename
Created October 29, 2010 17:04
Show Gist options
  • Save handlename/653904 to your computer and use it in GitHub Desktop.
Save handlename/653904 to your computer and use it in GitHub Desktop.
better is_deeply
<?php
function is_deeply($a, $b, $diff = array(), $key_str = '') {
ksort($a);
ksort($b);
$keys = array_unique(
array_merge(
array_keys($a),
array_keys($b)
)
);
foreach($keys as $key) {
$index = $key_str . '['. $key . ']';
// aがない
if(! isset($a[$key]) && ! is_null($a[$key])) {
$diff[$index] = array(
"undefined",
"$b[$key] (" . gettype($b[$key]) . ')',
);
continue;
}
// bがない
else if(! isset($b[$key]) && ! is_null($b[$key])) {
$diff[$index] = array(
"$a[$key] (" . gettype($a[$key]) . ')',
"undefined",
);
continue;
}
// 両方ある
else {
// 両方配列
if(is_array($a[$key]) && is_array($b[$key])) {
$diff = is_deeply($a[$key], $b[$key], $diff, $index);
continue;
}
// 一致しない
if($a[$key] !== $b[$key]) {
$diff[$index] = array(
"$a[$key] (" . gettype($a[$key]) . ')',
"$b[$key] (" . gettype($b[$key]) . ')',
);
continue;
}
// 一致する
else {
continue;
}
}
}
return $diff;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment