• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • array_udiff_uassoc()

    (PHP 5, PHP 7)

    带索引检查计算数组的差集,用回调函数比较数据和索引

    说明

    array_udiff_uassoc(array $array1,array $array2[,array $...],callable$value_compare_func,callable$key_compare_func): array

    array_udiff_uassoc()返回一个数组,该数组包括了所有在$array1中但是不在任何其它参数数组中的值。

    注意和array_diff()与array_udiff()不同的是键名也用于比较。

    参数

    $array1

    第一个数组。

    $array2

    第二个数组。

    $value_compare_func

    在第一个参数小于,等于或大于第二个参数时,该比较函数必须相应地返回一个小于,等于或大于 0 的整数。

    callback(mixed $a,mixed $b): int
    $key_compare_func

    对键名(索引)的检查也是由回调函数$key_compare_func进行的。这和array_udiff_assoc()的行为不同,后者是用内部函数比较索引的。

    返回值

    Returns anarraycontaining all the values from$array1that are not present in any of the other arguments.

    范例

    Example #1array_udiff_uassoc()例子

    <?php
    class cr {
        private $priv_member;
        function cr($val)
        {
            $this->priv_member = $val;
        }
        static function comp_func_cr($a, $b)
        {
            if ($a->priv_member === $b->priv_member) return 0;
            return ($a->priv_member > $b->priv_member)? 1:-1;
        }
        static function comp_func_key($a, $b)
        {
            if ($a === $b) return 0;
            return ($a > $b)? 1:-1;
        }
    }
    $a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),);
    $b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),);
    $result = array_udiff_uassoc($a, $b, array("cr", "comp_func_cr"), array("cr", "comp_func_key"));
    print_r($result);
    ?>
    

    以上例程会输出:

    Array
    (
        [0.1] => cr Object
            (
                [priv_member:private] => 9
            )
        [0.5] => cr Object
            (
                [priv_member:private] => 12
            )
        [0] => cr Object
            (
                [priv_member:private] => 23
            )
    )
    

    在上例中键值对"1"=> new cr(4)同时出现在两个数组中,因此不在本函数的输出中。要记住必须提供两个回调函数。

    注释

    Note:注意本函数只检查了多维数组中的一维。当然,可以用array_udiff_uassoc($array1[0],$array2[0],"data_compare_func","key_compare_func");来检查更深的维度。

    参见