Json对比

function getLeftArray($arr1, $arr2) {
    $result1 = [];

    if (is_leaf($arr1)) {
        if(is_assoc($arr1)) {
            foreach ($arr1 as $k => $v) {
                if(!isset($arr2[$k])) {
                    $newKey = "__removed__".$k;
                    $result1[$newKey] = $arr1[$k];
                }elseif($v!== $arr2[$k]) {
                    $newKey = "__changed__".$k;
                    $result1[$newKey] = $arr1[$k];
                }else{
                    $result1[$k] = $arr1[$k];
                }
            }
        }else{
            foreach ($arr1 as $k => $v) {
                if (!is_array($v)) {
                    if (!in_array($v, $arr2)) {
                        $result1[$k] = '__removed__'.$v;
                    } else {
                        $result1[$k] = $v;
                    }
                }
            }
        }
    } else {
        foreach ($arr1 as $key => $value) {
            //如果是索引数组
            if (!array_key_exists($key, $arr2)) {
                $newKey = "__removed__".$key;
                $result1[$newKey] = $arr1[$key];
            } else {
                if (!is_array($value) && $value !== $arr2[$key]) {
                    $newKey = "__changed__".$key;
                    $result1[$newKey] = $arr1[$key];
                } elseif (!is_array($value) && $value === $arr2[$key]) {
                    $result1[$key] = $arr1[$key];
                } elseif (is_array($value) && is_array($arr2[$key])) {
                    $sub = getLeftArray($arr1[$key], $arr2[$key]);
                    $result1[$key] = $sub;
                }
            }
        }
    }

    return $result1;
}

function is_leaf($array) {
    foreach ($array as $value) {
        if (is_array($value)) {
            return false;
        }
    }
    return true;
}

function is_assoc($array) {
    return array_keys($array) !== range(0, count($array) - 1);
}

function getRightArray($arr1, $arr2) {
    $result2 = [];

    if (is_leaf($arr2)) {
        if(is_assoc($arr2)) {
            foreach ($arr2 as $k => $v) {
                if(!isset($arr1[$k])) {
                    $newKey = "__added__".$k;
                    $result2[$newKey] = $arr2[$k];
                }elseif($v!== $arr1[$k]) {
                    $newKey = "__changed__".$k;
                    $result2[$newKey] = $arr2[$k];
                }else{
                    $result2[$k] = $arr2[$k];
                }
            }
        }else{
            foreach ($arr2 as $k => $v) {
                if (!is_array($v)) {
                    if (!in_array($v, $arr1)) {
                        $result2[$k] = '__added__'.$v;
                    } else {
                        $result2[$k] = $v;
                    }
                }
            }
        }
    } else {
        foreach ($arr2 as $key => $value) {
            //如果是索引数组
            if (!array_key_exists($key, $arr1)) {
                $newKey = "__added__".$key;
                $result2[$newKey] = $arr2[$key];
            } else {
                if (!is_array($value) && $value !== $arr1[$key]) {
                    $newKey = "__changed__".$key;
                    $result2[$newKey] = $arr2[$key];
                } elseif (!is_array($value) && $value === $arr1[$key]) {
                    $result2[$key] = $arr2[$key];
                } elseif (is_array($value) && is_array($arr1[$key])) {
                    $sub = getRightArray($arr1[$key], $arr2[$key]);
                    $result2[$key] = $sub;
                }
            }
        }
    }

    return $result2;
}

copy success
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113