array_diff_assoc

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

array_diff_assoc带索引检查计算数组的差集

说明

array_diff_assoc(array $array, array ...$arrays): array

array_diff_assoc() 返回一个数组,该数组包括了所有在 array 中但是不在任何其它参数 arrays 中的值。注意和 array_diff() 不同的是键名也用于比较。

参数

array

从这个数组进行比较

arrays

要比较的数组

返回值

返回一个 array,包含所有在 array 中但是不在任何其它参数数组中的值。

更新日志

版本 说明
8.0.0 现在可以仅使用一个参数调用此函数。以前,至少需要两个参数。

示例

示例 #1 array_diff_assoc() 示例

在此示例中,键值对 "a" => "green" 在两个数组中都有,因此不在本函数的输出中。不同的是,键值对 0 => "red" 出现在输出中,这是因为第一个数组的 "red" 的 key 自动分配为 0,而在第二个数组中,由于键名 0 已经被 yellow 占用,key 分配为 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);
?>

以上示例会输出:

Array
(
    [b] => brown
    [c] => blue
    [0] => red
)

示例 #2 array_diff_assoc() 示例

键值对 key => value 中的两个值仅在 (string) $elem1 === (string) $elem2 时被认为相等。也就是说使用了严格检查,字符串的表达必须相同。

<?php
$array1
= array(0, 1, 2);
$array2 = array("00", "01", "2");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>

以上示例会输出:

Array
(
    [0] => 0
    [1] => 1
)

注释

注意: 注意本函数只检查了多维数组中的一维。可以用 array_diff_assoc($array1[0], $array2[0]); 检查更深的维度。

注意: 使用更多的键比较相似数组时,确保参数传入的顺序是正确的。新的数组应该是在列表里的第一个。

参见

add a note add a note

User Contributed Notes 21 notes

up
62
Giosh
11 years ago
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
  }
}
up
13
contact at pascalopitz dot com
17 years ago
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
)
up
5
telefoontoestel at hotmail dot com
10 years ago
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;
}
?>
up
17
Michael Richey
11 years ago
If you're looking for a true array_diff_assoc, comparing arrays to determine the difference between two, finding missing values from both, you can use this along with array_merge.

$a = array('a'=>1,'b'=>2,'c'=>3);
$b = array('a'=>1,'b'=>2,'d'=>4);
print_r(array_diff_assoc($a,$b));
// returns:
array
(
    [c] => 3
)

print_r(array_diff_assoc($b,$a));
// returns
array
(
    [d] => 4
)

print_r(array_merge(array_diff_assoc($a,$b),array_diff_assoc($b,$a)));
// returns
array
(
    [c] => 3
    [d] => 4
)
up
11
chinello at gmail dot com
17 years ago
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).]
up
5
dev5er6 at gmail dot com
10 years ago
Works more like the original function:

<?php

function array_diff_assoc_recursive ( )
{
   
$args = func_get_args ( );
   
$diff = array ( );
    foreach (
array_shift ( $args ) as $key => $val )
    {
        for (
$i = 0, $j = 0, $tmp = array ( $val ) , $count = count ( $args ); $i < $count; $i++ )
            if (
is_array ( $val ) )
                if ( !isset (
$args[$i][$key] ) || !is_array ( $args[$i][$key] ) || empty( $args[$i][$key] ) )
                   
$j++;
                else
                   
$tmp[] = $args[$i][$key];
            elseif ( !
array_key_exists ( $key, $args[$i] ) || $args[$i][$key] !== $val )
               
$j++;
        if (
is_array ( $val ) )
        {
           
$tmp = call_user_func_array ( __FUNCTION__, $tmp );
            if ( ! empty (
$tmp ) ) $diff[$key] = $tmp;
            elseif (
$j == $count ) $diff[$key] = $val;
        }
        elseif (
$j == $count && $count ) $diff[$key] = $val;
    }

    return
$diff;
}

?>
up
4
tero dot lahtinen at iki dot fi
7 years ago
array_diff_assoc will fail, if a value is something that can not be converted to a string.
up
5
benjamin at moonfactory dot co dot jp
19 years ago
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 ;
    }
}

?>
up
6
Alexander Podgorny
18 years ago
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
?>
up
3
carl at thep dot lu dot se
21 years ago
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.
up
1
news_yodpeirs at thoftware dot de
13 years ago
A quite simple (yet not very efficient) way to compare the first level of arrays which have values that are not strings:
array_map('unserialize',array_diff_assoc(array_map('serialize',$arr1),array_map('serialize',$arr2)))
Might be useful for debugging (that's what I use it for).
up
1
jrajpu10 at gmail dot com
16 years ago
array_diff_assoc can also be used to find the duplicates in an array

<?php
$arr
= array('1','2','3','4','3','2','5');
$uniques = array_unique($arr);
// array_diff will not work here, array_diff_assoc works as it takes the key // in account.
$dups = array_diff_assoc($arr, $uniques);

print_r($dups);
?>

Note: The index of the $dups is not in strict sequential order as expected by C programmer.
up
2
cedric at daneel dot net
17 years ago
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;
    }
?>
up
2
spark dot crz at gmail dot com
6 years ago
Recursive implementation accepting multiple n-level-arrays as parameters:

<?php
function recursiveDiff() {
   
$arrs = func_get_args();
   
$first = array_shift($arrs);
   
$diff = [];

    foreach(
$first as $key => $value) {
       
$values = array_map(function($arr) use($key){
            if (
is_array($arr) && !array_key_exists($key, $arr))
                return
null;

            return
$arr[$key];
        },
$arrs);

        if (
in_array($value, $values))
            continue;

        if (
is_array($value)) {
           
array_unshift($values, $value);
           
$diff[$key] = call_user_func_array(__FUNCTION__, $values);
            continue;
        }

       
$diff[$key] = $first[$key];
    }

    return
$diff;
}
?>

The other attempt was cleaner but didn't work for all cases.
up
0
Henry
9 months ago
function array_diff_assoc_recursive($array1, $array2)
    {
        $array1Array = [];

        foreach ($array1 as $key => $value) {
            if (!is_array($value)) {
                continue;
            }
            $array1Array[$key] = $value;
            unset($array1[$key]);
        }

        $array2Array = [];

        foreach ($array2 as $key => $value) {
            if (!is_array($value)) {
                continue;
            }
            $array2Array[$key] = $value;
            unset($array2[$key]);
        }

        $diff = is_int(array_key_first($array1)) ? array_values(array_diff($array1, $array2)) : array_diff_assoc($array1, $array2);

        foreach ($array1Array as $key => $value) {
            if (isset($array2Array[$key])) {
                $subDiff = array_diff_assoc_recursive($value, $array2Array[$key]);
                if (count($subDiff) !== 0) {
                    $diff = array_merge($diff, is_int($key) ? [$subDiff] : [$key => $subDiff]);
                }
            } else {
                $diff = array_merge($diff, is_int($key) ? [$value] : [$key => $value]);
            }
        }

        return $diff;
    }
up
0
folurinyinka at gmail dot com
2 years ago
Recursive implementation accepting multiple n-level-arrays as parameters:

```php
function recursiveDiff(...$arrays)
{
    $first = array_shift($arrays);
    $diff = [];

    foreach ($first as $key => $value) {
        $values = array_map(function ($arr) use ($key) {
            if (is_array($arr) && !array_key_exists($key, $arr)) {
                return null;
            }

            return isset($arr[$key]) ? $arr[$key] : null;
        }, $arrs);

        if (in_array($value, $values)) {
            continue;
        }

        if (is_array($value)) {
            array_unshift($values, $value);
            $diff[$key] = call_user_func_array(__FUNCTION__, $values);
            continue;
        }

        $diff[$key] = $first[$key];
    }

    return $diff;
}
```
up
0
angujomondi at gmail dot com
3 years ago
For recursive diff of multiple arrays, exending solution provided by Gosh.

<?php

function array_diff_assoc_recursive(array $array, array ...$arrays)
    {
       
$func  = function($array1, $array2) use (&$func){
           
$difference = [];
            foreach (
$array1 as $key => $value) {
                if (
is_array($value)) {
                    if (!isset(
$array2[$key]) || !is_array($array2[$key])) {
                       
$difference[$key] = $value;
                    } else {
                       
$new_diff = $func($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;
        };
       
$diffs = $array;
        foreach (
$arrays as $_array) {
           
$diffs = $func($diffs, $_array);
        }
        return
$diffs;
    }

?>
up
-1
nekto
2 years ago
PHP associative keys are case-sensitive, key "a" is different from key "A":

<?php
$arr
= ["A" => 666];
var_dump($arr["a"]);
var_dump($arr["A"]);
?>

produces:
NULL
int(666)

And in the same way, array_diff_assoc treats keys case-sensitively, and key "A" is not equal to key "a" and will be diffed:

<?php
// in arrays below value of "a" and "A" are the same, but keys are different
// values of "b" are different
// values of "c" are the same
$compareWhat = ["a" => 666, "b" => 666, "c" => 666, ];
$compareWith = ["A" => 666, "b" => 667, "c" => 666, ];

var_dump(array_diff_assoc($compareWhat, $compareWith));
?>

produces:
array(2) {
["a"]=> int(666)
["b"]=> int(666)
}

And if the order of values in array is different, the order of the result will be different, although essentially the result stays the same:

<?php
// the same as above, but "b" comes before "a" in $compareWhat
$compareWhat = ["b" => 666, "a" => 666, "c" => 666, ];
$compareWith = ["A" => 666, "b" => 667, "c" => 666, ];

var_dump(array_diff_assoc($compareWhat, $compareWith));
?>

produces:
array(2) {
["b"]=> int(666)
["a"]=> int(666)
}
up
0
shadow_games at abv dot bg
10 years ago
there is a functiont that i searched long enough now i have created it so someone else to find it if he need it ;]
<?php
   
function compare_two_object_recursive($object_1, $object_2, $object_1_Identifier = false, $object_2_Identifier = false){
       
$object1 = (array)$object_1;
       
$object2 = (array)$object_2;
       
$object3 = array();

       
$o1i = $object_1_Identifier ? $object_1_Identifier : 1;
       
$o2i = $object_2_Identifier ? $object_2_Identifier : 2;

        foreach(
$object1 as $key => $value){
            if(
is_object($object1[$key])){
               
$object1[$key] = (array)$object1[$key];
               
$object2[$key] = (array)$object2[$key];
               
$object3[$key] = (object)compare_two_object_recursive($object1[$key], $object2[$key], $o1i, $o2i);
            }elseif(
is_array($object1[$key])){
               
$object3[$key] = compare_two_object_recursive($object1[$key], $object2[$key], $o1i, $o2i);
            }else{
                if(
$object1[$key] == $object2[$key]){
                   
$object3[$key]['comparison_status'] = "SAME";
                }else{
                   
$object3[$key]['comparison_status'] = "NOT THE SAME";
                   
$object3[$key][$o1i] = $object1[$key];
                   
$object3[$key][$o2i] = $object2[$key];
                }
            }
        }
        return
$object3;
    }
?>
up
-2
spark dot crz at gmail dot com
6 years ago
Yet another recursive implementation, without if-else hell and with multiple parameters just like the original.

<?php
function recursiveDiff() {
   
$arrs = func_get_args();
   
$diff = call_user_func_array('array_diff_assoc', $arrs);

    foreach(
$diff as $key => $value) {
        if (!
is_array($value))
            continue;

       
$children = array_map(function($arr) use($key){
            return
$arr[$key];
        },
$arrs);

       
$diff[$key] = call_user_func_array(__FUNCTION__, $children);
    }

    return
$diff;
}
up
-5
francois-php at ceonizme dot fr
10 years ago
to chinello at gmail dot com
I've had to use your function but it showed that the use of isset can't differentiate the NULL values from not setted values.

Here's a version that takes care of this subtility.

<?php
function array_diff_assoc_recursive($array1, $array2)
{
   
$difference = NULL;
    foreach(
$array1 as $key => $value)
    {
        if(
is_array($value))
        {
            if(!
array_key_exists($key, $array2))
            {
               
$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;
}
?>

Hope that helps
François
To Top