I need to be able to compare two three dimensional associative arrays. What I ultimately need to do is create a function that will return the differences between the two, if differences exist? I have tried many variations of the foreach construct, the for loop and the while loop but am having little or no concrete success. All I have been able to come up with so far is a function that will count the total number of elements in the arrays and compare that, but I need to know what the differences are if that check fails.
This is what I came up with to do the count, what follows is one attempt to do the comparison. Any help would be most appreciated. Thanks.
Alan
function countArray($arr)
{
reset($arr);
foreach($arr as $key => $value) {
// echo"key, $key : value, $value<br>\n";
++$count;
if (is_array($value)) {
$second_level = $value;
foreach ($second_level as $key => $value) {
// echo"key, $key : value, $value<br>\n";
++$count;
if (is_array($value)) {
$third_level = $value;
foreach ($third_level as $key => $value) {
// echo"key, $key : value, $value<br>\n";
++$count;
if (is_array($value)) {
$fourth_level = $value;
foreach ($fourth_level as $key => $value) {
// echo"key, $key : value, $value<br>\n";
++$count;
}
}
}
}
}
}
}
return $count;
}
My attempt:
function compareArrays($arr1, $arr2)
{
reset($arr1);
reset($arr2);
// check top level elements
foreach($arr1 as $key => $value) {
$result = array_diff($arr1, $arr2);
if ($result) {
echo"Arrays do not match up in first dimension<br>";
}
if (is_array($value)) {
$second_level = $value;
foreach($second_level as $key => $value) {
$array1 = $arr1[1];
$array2 = $arr2[1];
$result = array_diff($array1, $array2);
if ($result) {
echo"Arrays do not match up in second dimension<br>";
}
if (is_array($value)) {
$third_level = $value;
foreach($third_level as $key => $value) {
$result = array_diff($arr1[2], $arr2[2]);
if ($result) {
echo"Arrays do not match up in third dimension<br>";
}
if (is_array($value)) {
$fourth_level = $value;
foreach($third_level as $key => $value) {
$result = array_diff($arr1[3], $arr2[3]);
if ($result) {
echo"Arrays do not match up in fourth dimension<br>";
}
}
}
}
}
}
}
}
return $result;
}