(PHP 4 >= 4.3.0, PHP 5)
array_diff_assoc — 带索引检查计算数组的差集
$array1
, array $array2
[, array $ ...
] )
array_diff_assoc() 返回一个数组,该数组包括了所有在
array1
中但是不在任何其它参数数组中的值。注意和
array_diff() 不同的是键名也用于比较。
Example #1 array_diff_assoc() 例子
<?php
$array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array ("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
?>
以上例程会输出:
Array ( [b] => brown [c] => blue [0] => red )
上面的例子中可以看到键值对 "a" => "green" 在两个数组中都有,因此不在本函数的输出中。与此不同,键值对 0 => "red" 出现在输出中是因为第二个参数中的 "red" 的键名是 1。
键值对 key => value 中的两个值仅在 (string) $elem1 === (string) $elem2 时被认为相等。也就是说使用了严格检查,字符串的表达必须相同。
Note: 注意本函数只检查了多维数组中的一维。当然可以用 array_diff_assoc($array1[0], $array2[0]); 检查更深的维度。
参见 array_diff(), array_intersect() 和 array_intersect_assoc()。
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.
Example #2 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);
?>
以上例程会输出:
Array ( [b] => brown [c] => blue [0] => red )
Example #3 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);
?>
以上例程会输出:
Array ( [0] => 0 [1] => 1 )
Note: This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example, array_diff_assoc($array1[0], $array2[0]);.