递归和array_diff()? [英] recursive array_diff()?

查看:155
本文介绍了递归和array_diff()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在找一些工具给我两个数组的递归差异。我设想是有两个color- codeD树形结构的网页。每个树,绿色是匹配两个数组其中所述阵列的部分,红色为不匹配的其它各部件。像DBUG的输出

I'm looking for some tool to give me a recursive diff of two arrays. What I envision is a web page with two color-coded tree-structures. On each tree, green are parts of the array which match in both arrays, and red is for parts of each that don't match the other. Something like the output of dBug

我有一些code,它给了我一个嵌套数组来填充报告。我正在开发,应该是更快的新方法,但我需要测试的值,也是结构,以确保它给输出相同的旧方法。

I have some code that gives me a nested array to populate a report. I'm developing a new method that should be faster, but I need to test the values and also the structure, to make sure it gives output identical to the old method.

有什么在那里,我可以使用?或者我需要写?还是有别的办法来完成我的目标?

Is there something out there that I can use? Or do I need to write this? Or is there another way to accomplish my goals?

推荐答案

有中的和array_diff

function arrayRecursiveDiff($aArray1, $aArray2) {
  $aReturn = array();

  foreach ($aArray1 as $mKey => $mValue) {
    if (array_key_exists($mKey, $aArray2)) {
      if (is_array($mValue)) {
        $aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]);
        if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; }
      } else {
        if ($mValue != $aArray2[$mKey]) {
          $aReturn[$mKey] = $mValue;
        }
      }
    } else {
      $aReturn[$mKey] = $mValue;
    }
  }
  return $aReturn;
} 

的实施只处理在同一时间两个数组,但我不认为这真的拥有一种问题。如果你需要3个或更多阵列的差异在同一时间,你可以运行diff命令顺序。另外此方法使用密钥检查,并做了宽松的验证。

The implementation only handles two arrays at a time, but I do not think that really posses a problem. You could run the diff sequentially if you need the diff of 3 or more arrays at a time. Also this method uses key checks and does a loose verification.

这篇关于递归和array_diff()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆