array_uintersect通过自定义函数来比较值,计算数组的交集。
/**
* Computes the intersection of arrays, compares data by a callback function
* @link https://php.net/manual/en/function.array-uintersect.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 present in all the arguments.
* @meta
*/
function array_uintersect(array $array1, array $array2, array $_ = null, $data_compare_func) { }
示例:
$array1 = [
'c1' => 'ccc',
'a1' => 'aaa',
'b1' => 'bbb'
];
$array2 = [
'c2' => 'ccc',
'a2' => 'aaa',
'b2' => 'bbb2'
];
$result = array_uintersect($array1, $array2, function($v1, $v2){
//echo $v1.'--'.$v2."\n";
if ($v1 == $v2) {
return 0;
} elseif ($v1 < $v2) {
return -1;
} else {
return 1;
}
});
var_dump($result);
//结果
//array(2) {
// 'c1' =>
// string(3) "ccc"
// 'a1' =>
// string(3) "aaa"
//}