PHP函数array_diff_ukey

array_diff_ukey通过自定义函数来比较键,计算数组的差集 

/**
 * Computes the difference of arrays using a callback function on the keys for comparison
 * @link https://php.net/manual/en/function.array-diff-ukey.php
 * @param array $array1 <p>
 * The array to compare from
 * </p>
 * @param array $array2 <p>
 * An array to compare against
 * </p>
 * @param array $_ [optional]
 * @param callback $key_compare_func <p>
 * callback function to use.
 * The callback function 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 entries from
 * array1 that are not present in any of the other arrays.
 * @meta
 */
function array_diff_ukey(array $array1, array $array2, array $_ = null, $key_compare_func) { }

 

示例: 

$array1 = [
    'a' => 'aaa-1',
    'b' => 'bbb-1',
    'c' => 'ccc'
];

$array2 = [
    'a' => 'aaa-2',
    'b' => 'bbb-2',
    'e' => 'ccc'
];

$result = array_diff_ukey($array1, $array2, function($k1, $k2){
    if ($k1 == $k2) {
        return 0;
    }

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

var_dump($result);

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

上一篇:C# 之Richtextbox保存为Byte[] 和读取Byte[]


下一篇:C中的一些函数