difference between two arrays

To get the difference between the two arrays you need to do the following:

$fullDiff = array_merge(array_diff($array1, $array2), array_diff($array2, $array1));

The reason being that array_diff() will only give you the values that are in $array1 but not $array2, not the other way around. The above will give you both.


Note: this answer will return the values in $array2 that are not present in $array1, it will not return the values in $array1 that are not in $array2.

$diff = array_diff($array2, $array1);

array_diff()


<?php
function getArrayDiff($a1, $a2) {
    $result = array();

    print_r($a1);
    print_r($a2);

    // If First Array is Bigger than Second
    if( count($a1) > count($a2) ) {
        $result=array_diff($a1,$a2);
    }
    // If Second Array is Bigger than First
    if( count($a1) < count($a2) ) {
        $result=array_diff($a2,$a1);
    }
    // If Both array are same but, data values are different.
    else
    {
        $result = array_merge (array_diff($a2,$a1), array_diff($a1,$a2));   
    }
    return $result;
}

print "<pre>";
// First Array is Big
echo "First Array is Big <br/>";
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
print_r( getArrayDiff($a1, $a2) );

// Second Array is Big
echo "Second Array is Big <br/>";
$a1=array("e"=>"red","f"=>"green","g"=>"blue");
$a2=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
print_r( getArrayDiff($a1, $a2) );

// Both Array are same
echo "Both Array are same <br/>";
$a1=array("a"=>"red","b"=>"green","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
print_r( getArrayDiff($a1, $a2) );

?>

Output:

First Array is Big 
Array
(
    [a] => red
    [b] => green
    [c] => blue
    [d] => yellow
)
Array
(
    [e] => red
    [f] => green
    [g] => blue
)
Array
(
    [d] => yellow
)
Second Array is Big 
Array
(
    [e] => red
    [f] => green
    [g] => blue
)
Array
(
    [a] => red
    [b] => green
    [c] => blue
    [d] => yellow
)
Array
(
    [d] => yellow
)
Both Array are same 
Array
(
    [a] => red
    [b] => green
    [d] => yellow
)
Array
(
    [e] => red
    [f] => green
    [g] => blue
)
Array
(
    [g] => blue
    [d] => yellow
)

If you want to get difference between arrays recursively, try this function:

function arrayDiffRecursive($firstArray, $secondArray, $reverseKey = false)
{
    $oldKey = 'old';
    $newKey = 'new';
    if ($reverseKey) {
        $oldKey = 'new';
        $newKey = 'old';
    }
    $difference = [];
    foreach ($firstArray as $firstKey => $firstValue) {
        if (is_array($firstValue)) {
            if (!array_key_exists($firstKey, $secondArray) || !is_array($secondArray[$firstKey])) {
                $difference[$oldKey][$firstKey] = $firstValue;
                $difference[$newKey][$firstKey] = '';
            } else {
                $newDiff = arrayDiffRecursive($firstValue, $secondArray[$firstKey], $reverseKey);
                if (!empty($newDiff)) {
                    $difference[$oldKey][$firstKey] = $newDiff[$oldKey];
                    $difference[$newKey][$firstKey] = $newDiff[$newKey];
                }
            }
        } else {
            if (!array_key_exists($firstKey, $secondArray) || $secondArray[$firstKey] != $firstValue) {
                $difference[$oldKey][$firstKey] = $firstValue;
                $difference[$newKey][$firstKey] = $secondArray[$firstKey];
            }
        }
    }
    return $difference;
}

Test:

$differences = array_replace_recursive(
    arrayDiffRecursive($firstArray, $secondArray),
    arrayDiffRecursive($secondArray, $firstArray, true)
);
var_dump($differences);

Tags:

Php