PHP函数array_uintersect_uassoc

2021/11/15 14:09:54

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



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


扫一扫关注最新编程教程