array_uintersect_assoc通过自定义函数来比较值,使用内建函数比较键,计算数组的交集。
/**
* Computes the intersection of arrays with additional index check, compares data by a callback function
* @link https://php.net/manual/en/function.array-uintersect-assoc.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>
* For comparison is used the user supplied callback function.
* 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_assoc(array $array1, array $array2, array $_ = null, $data_compare_func) { }
示例:
$array1 = [
'c2' => 'ccc',
'a1' => 'aaa',
'b2' => 'bbb'
];
$array2 = [
'c2' => 'ccc',
'a2' => 'aaa',
'b2' => 'bbb2'
];
$result = array_uintersect_assoc($array1, $array2, function($v1, $v2){
if ($v1 == $v2) {
return 0;
}
return $v1 > $v2 ? 1 : -1;
});
var_dump($result);
//结果
//array(1) {
// 'c2' =>
// string(3) "ccc"
//}