Php 数组的特殊行为?

Php 数组的特殊行为?,php,arrays,Php,Arrays,我有以下Php脚本: <?php function filt($k, $l){ if($k===$l){ var_dump("valid: ".$k."-".$l); return 0; } return 1; } $a6=array(7, 9, 3, 33); $a7=array(2, 9, 3, 33); $u=array_udiff($a6, $a7, "filt"); var_dump($u); ?> 正如我所知,数组

我有以下Php脚本:

<?php
function filt($k, $l){
    if($k===$l){
        var_dump("valid: ".$k."-".$l);
        return 0;
    }
    return 1;
}
$a6=array(7, 9, 3, 33);
$a7=array(2, 9, 3, 33);
$u=array_udiff($a6, $a7, "filt");
var_dump($u);
?>
正如我所知,数组_udiff应该转储相等的值,并且只允许与第一个数组不同的值。 这里有什么问题吗?
我在Windows7上运行WampServer 2.2版。Php版本:5.3.9。

您没有返回所有必需的值(例如-1、0、1)。见:

$a6=数组(7,9,3,33);
$a7=数组(2,9,3,33);
$u=数组_udiff($a6,$a7,函数($k,$l){
返回$k>$l-1:($k<$l-1:0);
});
印刷费(美元);

您没有返回所有必需的值(例如-1、0、1)。见:

$a6=数组(7,9,3,33);
$a7=数组(2,9,3,33);
$u=数组_udiff($a6,$a7,函数($k,$l){
返回$k>$l-1:($k<$l-1:0);
});
印刷费(美元);

注意,文档中说:

The comparison 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.
你没那么做。要确保您这样做,只需使您的
filt
函数返回
$l-$k


对此有一个简单的解释:元素可能是任意顺序的。为了避免将每个元素与其他元素进行比较,它首先对它们进行排序。这就是为什么您需要+/0/-

注意,文档中说:

The comparison 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.
你没那么做。要确保您这样做,只需使您的
filt
函数返回
$l-$k


对此有一个简单的解释:元素可能是任意顺序的。为了避免将每个元素与其他元素进行比较,它首先对它们进行排序。这就是为什么您需要+/0/-

即使减法也足够了:
函数($k,$l){return$k-$l;}
:)这更奇怪,它不应该只通过返回1和0来工作吗?我的意思是,这是一个逻辑问题,如果相等返回0,其他值不同于0。难道它不应该从这个角度来区分吗?如果没有,为什么?@PLB这是我通常会做的,但我不太确定。因为问题中最初的代码是如何编写的。@AndrewG.H。也许吧,但这真的很重要吗?如果你想使用array_udiff,就按照它想使用的方式使用它。即使减法也足够了:
函数($k,$l){return$k-$l;}
:)更奇怪的是,它不应该只通过返回1和0来工作吗?我的意思是,这是一个逻辑问题,如果相等返回0,其他值不同于0。难道它不应该从这个角度来区分吗?如果没有,为什么?@PLB这是我通常会做的,但我不太确定。因为问题中最初的代码是如何编写的。@AndrewG.H。也许吧,但这真的很重要吗?如果您想使用array_udiff,请按照希望的方式使用它。感谢您解释了在回调函数中返回-1/0/1的不太明显的需要。感谢您解释了在回调函数中返回-1/0/1的不太明显的需要。
The comparison 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.