PHP array_udiff() function
Example
Compare the key values of two 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"); $a2 = array("a" => "blue", "b" => "black", "e" => "blue"); $result=array_udiff($a1, $a2, "myfunction"); print_r($result); ?>
Definition and Usage
The array_udiff() function is used to compare the key values of two (or more) arrays and returns the difference set.
Note:This function uses a user-defined function to compare key values!
This function compares the key values of two (or more) arrays and returns a difference array that includes all the values in the compared arrays (array1)中,但是不在任何其他参数数组(array2 或 array3 等等)中的键值。
Description
array_udiff() function returns an array that includes all values in the compared arrays but not in any other parameter arrays, with the key names unchanged.
array_udiff() function compares array_diff() function behaves differently; the latter uses an internal function for comparison.
The comparison of data is performed using the array_udiff() function's myfunction .myfunction The function takes two parameters to be compared. If the first parameter is less than the second, the function returns a negative number; if the two parameters are equal, it returns 0; if the first parameter is greater than the second, it returns a positive number.
Syntax
array_udiff(array1,array2,array3...myfunction)
参数 | 描述 |
---|---|
array1 | 必需。与其他数组进行比较的第一个数组。 |
array2 | 必需。与第一个数组进行比较的数组。 |
array3,... | 可选。与第一个数组进行比较的其他数组。 |
myfunction |
必需。字符串值,定义可调用的比较函数。 如果第一个参数小于等于或大于第二个参数,则比较函数必须返回小于等于或大于 0 的整数。 |
技术细节
返回值: | 返回差集数组,该数组包含所有在被比较的数组(array1)中,但是不在任何其他参数数组(array2 或 array3 等等)中的键值。 |
PHP 版本: | 5.1.0+ |
更多实例
例子 1
比较三个数组的键值(使用用户自定义函数比较键值),并返回差集:
<?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); ?>