Compare the differences in a multidimensional array

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

I want to reorganize the request, but I have to make sure that I do not change what it returns in any way.

So what I want to do is copy the original query and call it, save the results. then run the function again with my new request.

Scroll through the two arrays of results and compare them for any differences that are always the case (keys, values, missing entries, type differences, etc.).

What is the easiest way to do this?

Essentially, I know how to call two queries, etc.,

I think my real question is: at the end, when I have two of my arrays of results, how do I go about and compare them.

What I would like to finish is a result of type "print_r", located next to each other with a red line or similar, highlighting any differences.

0
source share
3 answers

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 (eg 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, use array_diff () and array_diff_assoc () . Or try the 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; } 
+4
source

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

+1
source

Why not write the results of each query into text files, and then compare the two text files with the diff ?

0
source

All Articles