比较多维数组中的差异 [英] Compare differences in Multidimensional array

查看:133
本文介绍了比较多维数组中的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当丑陋的查询,然后查询的结果使用php进行后处理,将每一行变成它自己的多维数组。

I have a rather ugly query, and the results from the query are then post-processed using php which turns each row into it's own multidimensional array.

重构查询,但需要确保我不以任何方式更改它返回的内容。

I want to refactor the query but need to make sure I do not change what it returns in any way.

所以我想做的是复制原始查询并调用,存储结果。
然后使用我的新查询再次运行该函数。

So What I want to do is copy the original query and call that, store the results. then run the function again with my new query.

循环两个结果数组,并比较它们之间的任何差异(键,值,

Loop over the two arrays of results and compare them for any differences what so ever (keys, values, missing entries, type differences etc).

这是最简单的方法吗?

如何调用两个查询等,

我想我的真正的问题是,一旦我有我的两个结果数组如何通过和比较他们。

I guess my real question is, at the end once I have my two arrays of the results how do I go through and compare them.

我最喜欢的是并排的print_r类型的输出,带有红线或类似的东西突出显示任何差异。

What I would love to end up with is a side by side "print_r" type output with a red line or similar going across highlighting any differences.

推荐答案

首先,您可以使用 array_uintersect_assoc()这样。

First of all, you can use array_uintersect_assoc() like this.

// First get intersecting values
$intersect = array_uintersect_assoc($expected, $results, "checkStructure");
print_r($intersect);

//Then print results that are in intersecting set (e.g. structure of $expected, value  of $results
print_r(array_uintersect_assoc($results, $intersect, "checkStructure"));

function checkStructure($x, $y) {
   if (!is_array($x) && !is_array($y)) {
      return 0;
   }
   if (is_array($x) && is_array($y)) {
       if (count($x) == count($y)) {
           foreach ($x as $key => $value) {
               if(array_key_exists($key,$y)) {
                   $x = checkStructure($value, $y[$key]);
                   if ($x != 0) return -1;
               } else {
                   return -1;
               }
           }
       }
   } else {
       return -1;
   }
   return 0;
}


b $ b

如果仍然没有,请参阅 array_diff()
array_diff_assoc()。或尝试以下代码。

If still not, take help of array_diff() and array_diff_assoc(). Or try following code.

function multidimensional_array_diff($a1,$a2) 
{ 
   $r = array(); 
   foreach ($a2 as $key => $second) 
   { 
      foreach ($a1 as $key => $first) 
      { 
         if (isset($a2[$key])) 
         { 
            foreach ($first as $first_value) 
            { 
               foreach ($second as $second_value) 
               { 
                   if ($first_value == $second_value) 
                   { 
                      $true = true; 
                      break;    
                   }    
               } 
               if (!isset($true)) 
               { 
                   $r[$key][] = $first_value; 
               } 
               unset($true); 
            } 
         } 
         else 
         { 
            $r[$key] = $first; 
         } 
      } 
   } 
   return $r; 
} 

这篇关于比较多维数组中的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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