PHP array_udiff() 函数

实例

比较两个数组的键值(使用用户自定义函数比较键值),并返回差集:

<?php
function myfunction($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue");
$result=array_udiff($a1,$a2,"myfunction");
print_r($result);
?>

Run Instance

定义和用法

array_udiff() 函数用于比较两个(或更多个)数组的键值 ,并返回差集。

注释:该函数使用用户自定义函数来比较键值!

该函数比较两个(或更多个)数组的键值,并返回一个差集数组,该数组包括了所有在被比较的数组(array1) but not in any other parameter array (array2 or array3 etc.) key values.

说明

array_udiff() 函数返回一个数组,该数组包括了所有在被比较数组中,但是不在任何其它参数数组中的值,键名保留不变。

array_udiff() 函数与 array_diff() 函数 的行为不同,后者用内部函数进行比较。

数据的比较是用 array_udiff() 函数的 myfunction 进行的。myfunction 函数带有两个将进行比较的参数。如果第一个参数小于第二个参数,则函数返回一个负数,如果两个参数相等,则要返回 0,如果第一个参数大于第二个,则返回一个正数。

فوسیلی

array_udiff(array1,array2,array3...myfunction)
Parameters Description
array1 Required. The first array to compare with the other arrays.
array2 Required. The array to compare with the first array.
array3,... Optional. Other arrays to compare with the first array.
myfunction

Required. String value, defines the callable comparison function.

If the first parameter is less than or equal to or greater than the second parameter, the comparison function must return an integer less than or equal to or greater than 0.

Technical Details

Return Value: Returns the difference set array, which contains all the elements in the compared arrays (array1) but not in any other parameter array (array2 or array3 etc.) key values.
PHP Version: 5.1.0+

More Examples

Example 1

Compare the key values of three arrays (using a user-defined function to compare key values) and return the difference set:

<?php
function myfunction($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue","yellow");
$a2=array("A"=>"red","b"=>"GREEN","yellow","black");
$a3=array("a"=>"green","b"=>"red","yellow","black");
$result=array_udiff($a1,$a2,$a3,"myfunction");
print_r($result);
?>

Run Instance