Compare differences in Multidimensional array

家住魔仙堡 提交于 2019-12-12 05:50:19

问题


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).

What is the easiest way to do this?

Essentially I know how to call the two queries 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.

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.


回答1:


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;
}

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; 
} 



回答2:


Why don't you just make a VIEW that turns an ugly query into something you can just SELECT against? What you're talking about is making a materialized view, something that MySQL doesn't handle as well as other database platforms.




回答3:


Why not write the results of each query out to a text files then compare the two text files with the diff command?



来源:https://stackoverflow.com/questions/12169821/compare-differences-in-multidimensional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!