试图了解array_udiff行为 [英] Trying to understand array_udiff behavior

查看:149
本文介绍了试图了解array_udiff行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们继续。为什么 array_udiff 第一个数组值的排序

Let's continue. Why array_udiff compares values of first array after sorting?

$compare = function($a, $b) use(&$iteration_count)
    {
    echo("$a : $b\n");
    $iteration_count++;
    return strcmp($a, $b);
    };

$a = array('a', 'b', 'c');
$b = array('x', 'y', 'z');


$iteration_count = 0;
echo "array_udiff:" . json_encode(array_udiff($a, $b, $compare)) . "\n";
echo "iterations: $iteration_count\n\n";

输出

b : a  // sorting $a started
c : b   
y : x  // sorting $b started
z : y
a : x  // comparison started
a : b  //                    -- what for?
b : x
b : c  //                    -- what for?
c : x
array_udiff:["a","b","c"]
iterations: 9

http://3v4l.org/3Me8o#v500

推荐答案

A [0] B [0] <比较之后/ code>将跳过 A 这等于 A [0] ,因为<$ C所有值$ C> B 不具有价值;看到这里

After the comparison of A[0] and B[0] it will skip all values in A that equal A[0], because B doesn't have that value; see here.

要做到这一点,就必须进行比较,至少 A [1] A [0] ;你可以通过在第一阵列中的微小变化观察此行为:

To do this, it must compare at least A[1] against A[0]; you can observe this behaviour by making a small change in the first array:

$a = array('a', 'a', 'b', 'c');

输出:

...
a : x
a : a <-- *
a : b
b : x
b : c
c : x

这篇关于试图了解array_udiff行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆