为什么这个 usort() 函数在某些版本的 PHP 上失败?

发布于 2024-07-13 15:52:57 字数 640 浏览 5 评论 0原文

我们编写了一些涉及 usort 的代码,该代码在我们的开发系统(PHP 5.2.8)上运行良好,但在我们的实时系统(PHP 5.2.0)上遇到问题:

// Sort by distance
usort($locations, 'Interpolator::sortByDistance');

调用方法(在同一类 Interpolator 中):

private static function sortByDistance($a, $b) {
    $return = 0;
    if($a['distance'] > $b['distance']) {
        $return = 1;
    } else if ($a['distance'] < $b['distance']) {
        $return = -1;
    }
    return $return;
}        

在我们的实时系统上系统中,这返回一个完全任意排序的数组,原始顺序被打乱,但仍然没有按距离排序。

我找不到任何与此问题相关的 5.2.0 和 5.2.8 之间修复的 PHP bug 的参考。

这个问题可能来自哪里? 我可以自己编写排序函数来解决这个问题吗?

We wrote some code involving usort which works fine on our development systems (PHP 5.2.8), but are experiencing a problem on our live systems (PHP 5.2.0):

// Sort by distance
usort($locations, 'Interpolator::sortByDistance');

calls the method (within the same class Interpolator):

private static function sortByDistance($a, $b) {
    $return = 0;
    if($a['distance'] > $b['distance']) {
        $return = 1;
    } else if ($a['distance'] < $b['distance']) {
        $return = -1;
    }
    return $return;
}        

On our live systems, this returns a completely arbitrarily sorted array, the original order is disturbed, but still not sorted by distance.

I cannot find any reference to a PHP bug fixed between 5.2.0 and 5.2.8 relevant to this problem.

Where might this problem be coming from? Can I fix this short of writing a sorting function myself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱自由 2024-07-20 15:52:57

我唯一能想到的是你应该使用这个:

usort($locations, array('Interpolator', 'sortByDistance'));

The only think I can think of is that you should be using this:

usort($locations, array('Interpolator', 'sortByDistance'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文