dongxie3681 2018-05-24 08:23
浏览 131
已采纳

如何比较两个多维数组?

I compare these two arrays:

$array1:

   array(3) {
          [0]=>
          array(10000) {
            ["12345"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 19:46:25"
              ["fileName"]=>
              string(46) "monkey.jpg"
              ["path"]=>
              string(149) "Volumes/animals/monkey.jpg"
              ["size"]=>
              string(7) "2650752"
            }
            ["678790"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 14:39:43"
              ["fileName"]=>
              string(45) "elephant.jpg"
              ["path"]=>
              string(171) "Volumes/animals/elephant.jpg"
              ["size"]=>
              string(7) "2306688"
            }
            ["23123"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 14:39:43"
              ["fileName"]=>
              string(45) "fred.jpg"
              ["path"]=>
              string(171) "Volumes/animals/fred.jpg"
              ["size"]=>
              string(7) "2306688"
            }
          }    
         [1]=>
          array(10000) {
            ["624245"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 19:46:25"
              ["fileName"]=>
              string(46) "duck.jpg"
              ["path"]=>
              string(149) "Volumes/animals/duck.jpg"
              ["size"]=>
              string(7) "2650752"
            }
            ["934522"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 14:39:43"
              ["fileName"]=>
              string(45) "lora.jpg"
              ["path"]=>
              string(171) "Volumes/animals/lora.jpg"
              ["size"]=>
              string(7) "2306688"
            }
            ["832452"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 14:39:43"
              ["fileName"]=>
              string(45) "sam.jpg"
              ["path"]=>
              string(171) "Volumes/animals/sam.jpg"
              ["size"]=>
              string(7) "2306688"
            }
          }
        [2]=>
          array(1500) {
            ["234214"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 19:46:25"
              ["fileName"]=>
              string(46) "chi.jpg"
              ["path"]=>
              string(149) "Volumes/animals/chi.jpg"
              ["size"]=>
              string(7) "2650752"
            }
            ["942134"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 14:39:43"
              ["fileName"]=>
              string(45) "ape.jpg"
              ["path"]=>
              string(171) "Volumes/animals/ape.jpg"
              ["size"]=>
              string(7) "2306688"
            }
            ["234234"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 14:39:43"
              ["fileName"]=>
              string(45) "cat.jpg"
              ["path"]=>
              string(171) "Volumes/animals/cat.jpg"
              ["size"]=>
              string(7) "2306688"
            }
        }
    }

$array2:

   array(3) {
          [0]=>
          array(10000) {
            ["12345"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 19:46:25"
              ["fileName"]=>
              string(46) "monkei.jpg"
              ["path"]=>
              string(149) "Volumes/animals/monkey.jpg"
              ["size"]=>
              string(7) "2650752"
            }
            ["678790"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 14:39:43"
              ["fileName"]=>
              string(45) "elephant.jpg"
              ["path"]=>
              string(171) "Volumes/animals/elephant.jpg"
              ["size"]=>
              string(7) "2306688"
            }
            ["23123"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 14:39:43"
              ["fileName"]=>
              string(45) "fred.jpg"
              ["path"]=>
              string(171) "Volumes/animals/fred.jpg"
              ["size"]=>
              string(7) "2306688"
            }
          }    
         [1]=>
          array(10000) {
            ["624245"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 19:46:25"
              ["fileName"]=>
              string(46) "duck.jpg"
              ["path"]=>
              string(149) "Volumes/animals/duck.jpg"
              ["size"]=>
              string(7) "2650752"
            }
            ["934522"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 14:39:43"
              ["fileName"]=>
              string(45) "lora.jpg"
              ["path"]=>
              string(171) "Volumes/animals/lora.jpg"
              ["size"]=>
              string(7) "2306688"
            }
            ["832452"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 14:39:43"
              ["fileName"]=>
              string(45) "sam.jpg"
              ["path"]=>
              string(171) "Volumes/animals/sam.jpg"
              ["size"]=>
              string(7) "2306688"
            }
          }
        [2]=>
          array(1500) {
            ["234214"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 19:46:25"
              ["fileName"]=>
              string(46) "chi.jpg"
              ["path"]=>
              string(149) "Volumes/animals/chi.jpg"
              ["size"]=>
              string(7) "2650752"
            }
            ["942134"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 14:39:43"
              ["fileName"]=>
              string(45) "ape.jpg"
              ["path"]=>
              string(171) "Volumes/animals/ape.jpg"
              ["size"]=>
              string(7) "2306688"
            }
            ["234234"]=>
            array(5) {
              ["dateTime"]=>
              string(19) "2016-10-12 14:39:43"
              ["fileName"]=>
              string(45) "cat.jpg"
              ["path"]=>
              string(171) "Volumes/animals/cat.jpg"
              ["size"]=>
              string(7) "2306688"
            }
        }
    }

I compare them with this function:

    function array_diff_assoc_recursive($array1, $array2){
                    foreach($array1 as $key => $value){
                        if(is_array($value)){
                            if(!isset($array2[$key])){
                                $difference[$key] = $value;
                            }
                            elseif(!is_array($array2[$key])){
                                $difference[$key] = $value;
                            }
                            else{
                                $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
                                if($new_diff != FALSE)
                                {
                                    $difference[$key] = $new_diff;
                                }
                            }
                        }
                        elseif(!isset($array2[$key]) || $array2[$key] != $value){
                            $difference[$key] = $value;
                        }
                    }
                    return !isset($difference) ? 0 : $difference;
                }


                echo "<pre>";
                print_r(array_diff_assoc_recursive($array1, $array2));
                echo "</pre>";

The result is:

array(1) {
  [0]=>
  array(1) {
    ["12345"]=>
    array(1) {
      ["fileName"]=>
      string(46) "monkey.jpg"
    }
  }
}

This is working well. But what I would need is, both of the difference values - the original and the new one like this:

array(1) {
  [0]=>
  array(1) {
    ["12345"]=>
    array(2) {
      ["fileName1"]=>
      string(46) "monkey.jpg"
      ["fileName2"]=>
      string(46) "monkei.jpg"
    }
  }
}

I tried to add this line $old_diff = array_diff_assoc_recursive($array2[$key], $value); but it did not work out.

  • 写回答

1条回答 默认 最新

  • dongliang1873 2018-05-24 08:36
    关注

    add something like:

    elseif(!isset($array2[$key]) || $array2[$key] != $value) {
        $difference[$key."1"] = $value;
        $difference[$key."2"] = $array2[$key];
    }
    

    doesn't work ?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 halcon根据玻璃面板纹路取区域
  • ¥15 HFSS设计小型化180度耦合器
  • ¥15 使用CInternetSession,CHttpFile读取网页文件时有些电脑上会卡住怎么办?
  • ¥15 水下机器人的半物理仿真研究
  • ¥15 微服务假死,一段时间后自动恢复,如何排查处理
  • ¥50 webrtc-streamer TCP rtsp
  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题