(PHP 5)
array_diff_uassoc — 用用户提供的回调函数做索引检查来计算数组的差集
$array1
, array $array2
[, array $...
], callback $key_compare_func
)
array_diff_uassoc() 返回一个数组,该数组包括了所有在
array1
中但是不在任何其它参数数组中的值。注意和
array_diff() 不同的是键名也用于比较。
此比较是通过用户提供的回调函数来进行的。如果认为第一个参数小于,等于,或大于第二个参数时必须分别返回一个小于零,等于零,或大于零的整数。这和 array_diff_assoc() 不同的是还使用了比较索引的内部函数。
Example #1 array_diff_uassoc() 例子
<?php
function key_compare_func($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b)? 1:-1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_uassoc($array1, $array2, "key_compare_func");
print_r($result);
?>
以上例程会输出:
Array ( [b] => brown [c] => blue [0] => red )
上面的例子中 "a" => "green" 出现在两个数组中因此不在函数的输出中。但是 0 => "red" 却在输出中,因为第二个参数中的 "red" 的键名是 1。
两个索引是否相同是由用户提供的回调函数来检查的。
Note: 注意本函数只检查了多维数组中的一维。当然可以用 array_diff_uassoc($array1[0], $array2[0], "key_compare_func"); 检查更深的维度。
参见 array_diff(), array_diff_assoc(), array_udiff(), array_udiff_assoc(), array_udiff_uassoc(), array_intersect(), array_intersect_assoc(), array_uintersect(), array_uintersect_assoc() 和 array_uintersect_uassoc()。
array1
The array to compare from
array2
An array to compare against
...
More arrays to compare against
key_compare_func
在第一个参数小于,等于或大于第二个参数时,该比较函数必须返回一个小于,等于或大于0的整数
Returns an array containing all the entries from
array1
that are not present in any of the other arrays.
Example #2 array_diff_uassoc() example
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
function key_compare_func($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b)? 1:-1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_uassoc($array1, $array2, "key_compare_func");
print_r($result);
?>
以上例程会输出:
Array ( [b] => brown [c] => blue [0] => red )
The equality of 2 indices is checked by the user supplied callback function.
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_uassoc($array1[0], $array2[0], "key_compare_func");.