了解array_udiff() [英] Understanding array_udiff()

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

问题描述

我有以下简单的功能,试图了解 array_udiff()函数

I have the following simple function to try and understand the array_udiff() function

function udiffCompare( $value, $value2 )
{

echo $value . " - " . $value2 . "<br />";

}

$newArray   =   array(  

                    'value2' => 2, 
                    'value3' => 3, 
                    'value4' => 4
                );

$newArray2  =   array(  

                    'value2' => 2, 
                    'value3' => 3, 
                    'value4' => 4
                );

array_udiff( $newArray, $newArray2, 'udiffCompare' );

我希望这只是返回:

I would expect this to simply return:

2 - 2
3 - 3
4 - 4

然而,它返回:

3 - 2
4 - 3
3 - 2
4 - 3
4 - 4
4 - 3
4 - 3
3 - 2

这使我相信有些事情我真的不理解这里如何 array_udiff()的作品。

This leads me to believe there is something I am really not understanding here about how array_udiff() works.

即使我将其替换上面的echo语句:

Even if I replace the echo statement above with:

如果($价值== $值2){返回1; }其他{返回0; }

输出的阵列完全是空的,即使传递给函数的所有值都是平等的。

the outputted array is completely empty even though all the values passed to the function are equal.

有人能照亮一盏灯吗?

推荐答案

array_udiff 计算的区别的两个阵列。这是 $ newArray 所有条目,不能在 $ newArray2 。在这种情况下,结果是一个空数组,因为没有差别。

array_udiff computes the difference of the two arrays. That is all entries in $newArray which are not in $newArray2. In this case the result is an empty array because there is no difference.

您所看到的输出是因为你呼应正在被比较的每个值。究其原因,这是一个大的名单是因为,为了找出差异 array_udiff 的每一个值 $ newArray $ newArray2 的每个值。

The output you are seeing is because you are echoing each value that is being compared. The reason this is a larger list is because, in order to find out the difference array_udiff has to compare every value in $newArray with every value in $newArray2.

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

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