PHP array_diff_uassoc() 函数
实例
比较两个数组的键名和键值(使用用户自定义函数来比较键名),并返回差集:
<?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("d"=>"red","b"=>"green","e"=>"blue"); $result=array_diff_uassoc($a1,$a2,"myfunction"); print_r($result); ?>
定义和用法
array_diff_uassoc() 函数用于比较两个(或更多个)数组的键名和键值 ,并返回差集。
注释:该函数使用用户自定义函数来比较键名!
该函数比较两个(或更多个)数组的键名和键值,并返回一个差集数组,该数组包括了所有在被比较的数组(array1but not in any other parameter array (array2 or array3 etc.) keys and values.
语法
array_diff_uassoc(array1,array2,array3...,myfunction);
参数 | 描述 |
---|---|
array1 | 必需。与其他数组进行比较的第一个数组。 |
array2 | 必需。与第一个数组进行比较的数组。 |
array3,... | 可选。与第一个数组进行比较的其他数组。 |
myfunction | 必需。定义可调用比较函数的字符串。如果第一个参数小于、等于或大于第二个参数,则该比较函数必须返回小于、等于或大于 0 的整数。 |
说明
array_diff_uassoc() 函数使用用户自定义的回调函数 (callback) 做索引检查来计算两个或多个数组的差集。返回一个数组,该数组包括了在 array1 but not in any other parameter array.
Note that, unlike the array_diff() function, the keys are also compared.
The parameter myfunction is a user-defined function used to compare two arrays, which must have two parameters - i.e., the two keys to be compared. Therefore, it is exactly opposite in behavior to the function array_diff_assoc(), which uses an internal function for comparison.
The keys in the returned array remain unchanged.
Technical Details
Return value: | Returns the difference set array, which includes all the values in the compared arrays (array1but not in any other parameter array (array2 or array3 etc.) keys and values. |
PHP Version: | 5+ |
More Examples
Example 1
Compare the keys and values of three arrays (using a user-defined function to compare the keys) 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"=>"red","b"=>"green","d"=>"blue"); $a3=array("e"=>"yellow","a"=>"red","d"=>"blue"); $result=array_diff_uassoc($a1,$a2,$a3,"myfunction"); print_r($result); ?>