Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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;
- }
Add Comment
Please, Sign In to add comment