(PHP 5)
array_udiff — 用回调函数比较数据来计算数组的差集
array_udiff() 返回一个数组,该数组包括了所有在
array1
中但是不在任何其它参数数组中的值。注意键名保持不变。数据的比较是用
data_compare_func
进行的。如果认为第一个参数小于,等于,或大于第二个参数时必须分别返回一个小于零,等于零,或大于零的整数。这和
array_diff() 不同,后者使用了内部函数来比较数据。
Example #1 array_udiff() 例子
<?php
class cr {
private $priv_member;
function cr($val)
{
$this->priv_member = $val;
}
function comp_func_cr($a, $b)
{
if ($a->priv_member === $b->priv_member) return 0;
return ($a->priv_member > $b->priv_member)? 1:-1;
}
}
$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),);
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),);
$result = array_udiff($a, $b, array("cr", "comp_func_cr"));
print_r($result);
?>
以上例程会输出:
Array ( [0.5] => cr Object ( [priv_member:private] => 12 ) [0] => cr Object ( [priv_member:private] => 23 ) )
Note: 注意本函数只检查了多维数组中的一维。当然,可以用 array_udiff($array1[0], $array2[0], "data_compare_func"); 来检查更深的维度。
参见 array_diff(), array_diff_assoc(), array_diff_uassoc(), array_udiff_assoc(), array_udiff_uassoc(), array_intersect(), array_intersect_assoc(), array_uintersect(), array_uintersect_assoc() 和 array_uintersect_uassoc()。
array1
The first array.
array2
The second array.
data_compare_func
The callback comparison function.
在第一个参数小于,等于或大于第二个参数时,该比较函数必须返回一个小于,等于或大于0的整数
Returns an array containing all the values of array1
that are not present in any of the other arguments.
Example #2 array_udiff() example
<?php
class cr {
private $priv_member;
function cr($val)
{
$this->priv_member = $val;
}
static function comp_func_cr($a, $b)
{
if ($a->priv_member === $b->priv_member) return 0;
return ($a->priv_member > $b->priv_member)? 1:-1;
}
}
$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),);
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),);
$result = array_udiff($a, $b, array("cr", "comp_func_cr"));
print_r($result);
?>
以上例程会输出:
Array ( [0.5] => cr Object ( [priv_member:private] => 12 ) [0] => cr Object ( [priv_member:private] => 23 ) )
Note: Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_udiff($array1[0], $array2[0], "data_compare_func");.