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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀