dqifn68206 2014-05-26 10:15
浏览 55
已采纳

php比较两个嵌套数组并预测差异

How find difference of the two array

// ARRAY 1

$a1 = array(
    "a1" => 1,
    "a3" => array(
        "a31" => 31
    ),
    "a4" => array(
        "a41" => 41
    ),
    "a5" => array(
        "a51" => 51,
        "a52" => 52
    )
);

// ARRAY 2
$a2 = array(
    "a1" => 1,
    "a2" => 2,
    "a3" => array(
        "a31" => 31,
        "a32" => array(
            "a321" => 321,
            "a322" => 322
        )
    ),
    "a4" => array(
        "a41" => 42
    ),
    "a5" => array(
        "a51" => 51,
        "a52" => 52
    )
);

array_diff function returns ==> Array ( )

But there is a lot of difference exist in the above two array variables.

example:

  1. in ARRAY1 There is no key a2 but in ARRAY2 a2 is available.
  2. ARRAY2 a3 contain nested array (one nested level ) but in ARRAY 2 contain two nested sub level.

So here i want to compare two array n-level (ie. nested array)

  • 写回答

1条回答 默认 最新

  • douquqiang1513 2014-05-26 10:20
    关注
    $results = array_diff(array_map('serialize',$a2),array_map('serialize',$a1));
    $results = array_map('unserialize',$results);
    
    echo '<pre>';
    print_r($results);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52