PHP函数array_uintersect_uassoc

array_uintersect_uassoc通过自定义函数来比较值,使用自定义函数比较键,计算数组的交集。

/**
 * Computes the intersection of arrays with additional index check, compares data and indexes by a callback functions
 * @link https://php.net/manual/en/function.array-uintersect-uassoc.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>
 * @param callback $key_compare_func <p>
 * Key comparison callback function.
 * </p>
 * @return array an array containing all the values of
 * array1 that are present in all the arguments.
 * @meta
 */
function array_uintersect_uassoc(array $array1, array $array2, array $_ = null, $data_compare_func, $key_compare_func) { }

 示例:

$array1 = [
    'c2' => 'ccc',
    'a1' => 'aaa',
    'b2' => 'bbb'
];

$array2 = [
    'c2' => 'ccc',
    'a2' => 'aaa',
    'b2' => 'bbb2'
];

$result = array_uintersect_uassoc($array1, $array2, function($v1, $v2){
    if ($v1 == $v2) {
        return 0;
    }

    return $v1 > $v2 ? 1 : -1;
}, function($k1, $k2){
    if ($k1 == $k2) {
        return 0;
    }

    return $k1 > $k2 ? 1 : -1;
});

var_dump($result);

//结果
//array(1) {
//  'c2' =>
//  string(3) "ccc"
//}

上一篇:GDI+中发生一般性错误的解决办法


下一篇:[Android]组件之进度条2