Documentation on array_diff_assoc
array_diff_assoc = Computes the difference of arrays with additional index check
Compares array1 against array2 and returns the difference. Unlike array_diff() the array keys are also used in the comparison.
array1 The array to compare from array2 An array to compare against ... More arrays to compare against
Usage, params, and more on array_diff_assoc
array array_diff_assoc ( array $array1
, array $array2
[, array $...
] )
array1
The array to compare from array2
An array to compare against ...
More arrays to compare against
Returns an array containing all the values from array1
that are not present in any of the other arrays.
Notes and warnings on array_diff_assoc
Basic example of how to use: array_diff_assoc
Example #1 array_diff_assoc() example
In this example you see the "a" => "green" pair is present in both arrays and thus it is not in the output from the function. Unlike this, the pair 0 => "red" is in the output because in the second argument "red" has key which is 1.
<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>
The above example will output:
Array ( [b] => brown [c] => blue [0] => red )
Example #2 array_diff_assoc() example
Two values from key => value pairs are considered equal only if (string) $elem1 === (string) $elem2 . In other words a strict check takes place so the string representations must be the same.
<?php
$array1 = array(0, 1, 2);
$array2 = array("00", "01", "2");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>
The above example will output:
Array ( [0] => 0 [1] => 1 )
Other code examples of array_diff_assoc being used
The array_diff_assoc_array from "chinello at gmail dot com" (and others) will not work for arrays with null values. That's because !isset is true when an array key doesn't exists or is set to null.
(sorry for the changed indent-style)
<?php
function array_diff_assoc_recursive($array1, $array2) {
$difference=array();
foreach($array1 as $key => $value) {
if( is_array($value) ) {
if( !isset($array2[$key]) || !is_array($array2[$key]) ) {
$difference[$key] = $value;
} else {
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if( !empty($new_diff) )
$difference[$key] = $new_diff;
}
} else if( !array_key_exists($key,$array2) || $array2[$key] !== $value ) {
$difference[$key] = $value;
}
}
return $difference;
}
?>
And here an example (note index 'b' in the output):
<?php
$a1=array( 'a' => 0, 'b' => null, 'c' => array( 'd' => null ) );
$a2=array( 'a' => 0, 'b' => null );
var_dump( array_diff_assoc_recursive( $a1, $a2 ) );
var_dump( chinello_array_diff_assoc_recursive( $a1, $a2 ) );
?>
array(1) {
["c"]=>
array(1) {
["d"]=>
NULL
}
}
array(2) {
["b"]=>
NULL
["c"]=>
array(1) {
["d"]=>
NULL
}
}
The following will recursively do an array_diff_assoc, which will calculate differences on a multi-dimensional level. This not display any notices if a key don't exist and if error_reporting is set to E_ALL:
<?php
function array_diff_assoc_recursive($array1, $array2)
{
foreach($array1 as $key => $value)
{
if(is_array($value))
{
if(!isset($array2[$key]))
{
$difference[$key] = $value;
}
elseif(!is_array($array2[$key]))
{
$difference[$key] = $value;
}
else
{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value)
{
$difference[$key] = $value;
}
}
return !isset($difference) ? 0 : $difference;
}
?>
[NOTE BY danbrown AT php DOT net: This is a combination of efforts from previous notes deleted. Contributors included (Michael Johnson), (jochem AT iamjochem DAWT com), (sc1n AT yahoo DOT com), and (anders DOT carlsson AT mds DOT mdh DOT se).]
The direction of the arguments does actually make a difference:
<?php
$a = array(
'x' => 'x',
'y' => 'y',
'z' => 'z',
't' => 't',
);
$b = array(
'x' => 'x',
'y' => 'y',
'z' => 'z',
't' => 't',
'g' => 'g',
);
print_r(array_diff_assoc($a, $b));
print_r(array_diff_assoc($b, $a));
?>
echoes:
Array
(
)
Array
(
[g] => g
)
Hi all,
For php versions < 4.3...
<?php
/**
* array_diff_assoc for version < 4.3
**/
if (!function_exists('array_diff_assoc'))
{
function array_diff_assoc($a1, $a2)
{
foreach($a1 as $key => $value)
{
if(isset($a2[$key]))
{
if((string) $value !== (string) $a2[$key])
{
$r[$key] = $value;
}
}else
{
$r[$key] = $value;
}
}
return $r ;
}
}
?>
To unset elements in an array if you know the keys but not the values, you can do:
<?php
$a = array("foo", "bar", "baz", "quux");
$b = array(1, 3); // Elements to get rid of
foreach($b as $e)
unset($a[$e]);
?>
Of course this makes most sense if $b has many elements or is dynamically generated.
To diff between n-dimensional array, juste use this :
<?php
function array_diff_values($tab1, $tab2)
{
$result = array();
foreach($tab1 as $values) if(! in_array($values, $tab2)) $result[] = $values;
return $result;
}
?>
NOTE: the diff_array also removes all the duplicate values that match to the values in the second array:
<?php
$array1 = array("a","b","c","a","a");
$array2 = array("a");
$diff = array_diff($array1,$array2);
// yields: array("b","c") the duplicate "a" values are removed
?>
an earlier post for recursive array_diff_assoc failed because isset returned false on an array element containing a null value. I updated the code so it compares null values too.
<?php
function array_diff_assoc_recursive($array1, $array2)
{
foreach($array1 as $key => $value)
{
if(is_array($value))
{
if(!isset($array2[$key]))
{
$difference[$key] = $value;
}
elseif(!is_array($array2[$key]))
{
$difference[$key] = $value;
}
else
{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!array_key_exists($key, $array2) || $array2[$key] != $value)
{
$difference[$key] = $value;
}
}
return !isset($difference) ? 0 : $difference;
}
?>