PHP教程

PHP函数array_diff_ukey

本文主要是介绍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"
//}

这篇关于PHP函数array_diff_ukey的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!