简体   繁体   中英

Compare differences in Multidimensional array

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.

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

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.

为什么不将每个查询的结果写到一个文本文件中,然后使用diff命令比较这两个文本文件?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM