PHP:检查两个multidim数组之间的差异

PHP:检查两个multidim数组之间的差异

问题描述:

Here's the two arrays dump:

array(2) { 
    [0]=> array(7) { 
        ["id"]=> string(1) "1" 
        ["shoppinglist_id"]=> string(1) "1" 
        ["product_id"]=> string(1) "1" 
        ["amount"]=> string(1) "5" 
        ["date_added"]=> string(10) "1326912709"
        ["name"]=> string(7) "Tunfisk" 
        ["supplier_id"]=> string(1) "2" 
    } 
    [1]=> array(7) { 
        ["id"]=> string(1) "2" 
        ["shoppinglist_id"]=> string(1) "1" 
        ["product_id"]=> string(1) "2" 
        ["amount"]=> string(1) "5" 
        ["date_added"]=> string(10) "1326912713" 
        ["name"]=> string(3) "Lax" 
        ["supplier_id"]=> string(1) "6" 
    } 
}

array(2) { 
    [0]=> array(7) { 
        ["id"]=> string(1) "5" 
        ["shoppinglist_id"]=> string(1) "3" 
        ["product_id"]=> string(1) "1" 
        ["amount"]=> string(1) "5" 
        ["date_added"]=> string(10) "1326912810" 
        ["name"]=> string(7) "Tunfisk" 
        ["supplier_id"]=> string(1) "2" 
    } 
    [1]=> array(7) { 
        ["id"]=> string(1) "6" 
        ["shoppinglist_id"]=> string(1) "3" 
        ["product_id"]=> string(1) "2" 
        ["amount"]=> string(1) "5" 
        ["date_added"]=> string(10) "1326912810" 
        ["name"]=> string(3) "Lax" 
        ["supplier_id"]=> string(1) "6" 
    } 
}

I tried to do array_diff() but this does not support multi-dimensional arrays.

So then i tried this function:

public 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_arraykey => $first_value)
                {

                    foreach ($second as $second_value)
                    {
                        if ($first_value == $second_value)
                        {
                            $true = true;
                            break;   
                        }
                    }
                    if (!isset($true))
                    {
                        if($first_arraykey != "date_added" && $first_arraykey != "shoppinglist_id")
                        {
                            $r[$key][$first_arraykey] = $first_value;
                        }

                    }
                    unset($true);
                }
            }
            else
            {
                $r[$key] = $first;
            }
      }
 }
  return $r;
}

This does not work either, returns me differences, that arent different.

Note as you can see it does not add to the difference array if the array key are date_added and shoppinglist_id (because, it's OK in my system that these are different).

The above should output an empty difference array, since there are no difference between those array (if we dont look at date_added and shoppinglist_id).

How can i make this work properly?

In the comments to array_diff, some recursive examples are given. This one seems to do, what you are searching for:

<?php 
function arrayRecursiveDiff($aArray1, $aArray2) { 
    $aReturn = array(); 

    foreach ($aArray1 as $mKey => $mValue) { 
        if (array_key_exists($mKey, $aArray2)) { 
            if (is_array($mValue)) { 
                $aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]); 
                if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; } 
            } else { 
                if ($mValue != $aArray2[$mKey]) { 
                    $aReturn[$mKey] = $mValue; 
                } 
            } 
        } else { 
            $aReturn[$mKey] = $mValue; 
        } 
    } 

    return $aReturn; 
} 
?>

Source: http://www.php.net/manual/en/function.array-diff.php#91756