array_udiff通过自定义函数来比较值,计算数组的差集?
/**
* Computes the difference of arrays by using a callback function for data comparison
* @link https://php.net/manual/en/function.array-udiff.php
* @param array $array1 <p>
* The first array.
* </p>
* @param array $array2 <p>
* The second array.
* </p>
* @param array $_ [optional]
* @param callback $data_compare_func <p>
* The callback comparison function.
* </p>
* <p>
* The user supplied callback function is used for comparison.
* It must return an integer less than, equal to, or greater than zero if
* the first argument is considered to be respectively less than, equal
* to, or greater than the second.
* </p>
* @return array an array containing all the values of array1
* that are not present in any of the other arguments.
* @meta
*/
function array_udiff(array $array1, array $array2, array $_ = null, $data_compare_func) { }
?示例:
$array1 = [
'a' => 'aaa-1',
'b' => 'bbb-1',
'c' => 'ccc'
];
$array2 = [
'a' => 'aaa-2',
'b' => 'bbb-2',
'e' => 'ccc'
];
$result = array_udiff($array1, $array2, function($v1, $v2){
if ($v1 == $v2) {
return 0;
}
return $v1 > $v2 ? 1 : -1;
});
var_dump($result);
//结果
//array(2) {
// 'a' =>
// string(5) "aaa-1"
// 'b' =>
// string(5) "bbb-1"
//}
|