误解了array_udiff的行为 [英] Misunderstanding the behavior of array_udiff
问题描述
我很难理解array_udiff
的工作原理.
根据文档:
array_udiff ($array1, $array2, data_compare_func)
[...]
data_compare_func
函数必须返回小于,等于或大于零的整数,如果认为第一个参数分别小于,等于或大于第二个参数.
因此考虑此功能:
function please_compare($a, $b) {
return $a !== $b;
};
如果$ a等于$ b,则该方法将返回0,否则返回1(因为此)./p>
(这里没有返回-1,我感觉解释是从那里来的,但是我可以比较一下值是不同的,不是一个大于另一个.)
有人可以在以下代码段中解释我array_udiff
的行为吗?我还包含了array_diff
的输出,实际上是我所期望的行为吗?
$array1 = array('a', 'b', 'c', 'd');
$array2 = array('a', 'b', 'c');
print_r(array_udiff($array1, $array2, 'please_compare'));
/* Returns:
Array
(
[0] => a
[1] => b
[3] => d
)
*/
print_r(array_diff($array1, $array2));
/* Returns:
Array
(
[3] => d
)
*/
array_udiff
依赖于比较函数返回适当的值,因为它对数组的元素进行排名.如果将一些输出添加到比较函数中,您将看到array_udiff
首先确定两个数组的排序顺序,只有这样做后,才开始比较array1元素和array2元素.通过从比较函数返回1,您将告诉array_udiff
'a'>'b'和'b'>'a',并且对两个数组中的所有其他元素也是如此.在您的特定情况下,这会导致array_udiff
认为array1中的 everything > array2中的 everything ,直到最终碰巧将array1中的'c'与' c'在array2中,并从函数中返回0(这就是为什么将'c'留在结果中的原因).有关array_udiff
内部工作的演示,请参见此 PHP小提琴.
I am having troubles to understand how array_udiff
works.
According to the documentation:
array_udiff ($array1, $array2, data_compare_func)
[...]
data_compare_func
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.
So considering this function:
function please_compare($a, $b) {
return $a !== $b;
};
if $a equals $b, the method will return 0, 1 otherwise (because of this).
(There is no -1 returned here and I have the feeling that the explanation comes from there but I can just compare that the values are different, not that one is greater than the other one.)
Could someone explain me array_udiff
's behavior in the following snippet? I also included the output of array_diff
, which is actually the behavior I was expecting?
$array1 = array('a', 'b', 'c', 'd');
$array2 = array('a', 'b', 'c');
print_r(array_udiff($array1, $array2, 'please_compare'));
/* Returns:
Array
(
[0] => a
[1] => b
[3] => d
)
*/
print_r(array_diff($array1, $array2));
/* Returns:
Array
(
[3] => d
)
*/
array_udiff
relies on the comparison function returning appropriate values, because it ranks the elements of your arrays. If you add some output to your comparison function, you will see that array_udiff
first determines the sort order for both arrays, and only after it has done this does it start comparing array1 elements to array2 elements. By returning 1 from your comparison function, you are telling array_udiff
that 'a' > 'b' and 'b' > 'a', and similarly for all other elements in both arrays. In your particular case, this causes array_udiff
to think that everything in array1 > everything in array2, until it finally happens to compare the 'c' in array1 to the 'c' in array2, and gets 0 back from your function (this is why it left 'c' out of the result). See this PHP fiddle for a demonstration of the internal working of array_udiff
.
这篇关于误解了array_udiff的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!