php usort():数组被用户比较函数修改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3235387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:06:32  来源:igfitidea点击:

usort(): Array was modified by the user comparison function

phpusort

提问by Michael

I have a web application that runs fine on our Linux servers but when running on Mac OS with the Zend Community Edition Server using PHP 5.3 we get the error:

我有一个 Web 应用程序可以在我们的 Linux 服务器上正常运行,但是在使用 PHP 5.3 的 Zend 社区版服务器在 Mac OS 上运行时,我们收到错误:

usort(): Array was modified by the user comparison function

usort():数组被用户比较函数修改

every time a page loads for the first time (it takes about 2 minutes for a page to tick over and load, on the linux servers the page loads in 1 second).

每次第一次加载页面时(页面滚动和加载大约需要 2 分钟,在 linux 服务器上,页面加载时间为 1 秒)。

Has anyone else experienced this or has any idea how I can fix the problem, I have tried playing around with PHP and Apache memory settings with no luck.

有没有其他人经历过这个或知道我如何解决这个问题,我尝试过使用 PHP 和 Apache 内存设置但没有运气。

回答by Achronos

There is a PHP bug that can cause this warning, even if you don't change the array.

有一个 PHP 错误会导致此警告,即使您不更改数组。

Short version, if any PHP debug functions examine the sort array, they will change the reference count and trick usort()into thinking you've changed the data.

简短版本,如果任何 PHP 调试函数检查排序数组,它们将更改引用计数并欺骗usort()您认为您已更改数据。

So you will get that warning by doing any of the following in your sort function (or any code called from it):

因此,您将通过在排序函数(或从它调用的任何代码)中执行以下任何操作来获得该警告:

  • calling var_dumpor print_ron any of the sort data
  • calling debug_backtrace()
  • throwing an exception -- any exception -- or even just creating an exception
  • 调用var_dumpprint_r任何排序数据
  • 打电话 debug_backtrace()
  • 抛出异常——任何异常——甚至只是创建一个异常

The bug affects all PHP 5 versions >= 5.2.11 but does not affect PHP >= 7. See the bug reportfor further details.

该错误影响所有 PHP 5 版本 >= 5.2.11,但不影响 PHP >= 7。有关详细信息,请参阅错误报告

As far as I can see, the only workaround is either "don't do that" (which is kind of hard for exceptions), or use the error suppression operator @usort()to ignore all errors.

据我所知,唯一的解决方法是“不要那样做”(这对于异常来说有点困难),或者使用错误抑制运算符@usort()来忽略所有错误。

回答by panditharshad

To resolve this issue we can handle as below

为了解决这个问题,我们可以处理如下

1) use error_reporting

1)使用error_reporting

$a = array('id' => 2,'val' => 3, 'ind' => 3);
$errorReporting = error_reporting(0);
usort($a);
error_reporting($errorReporting);

2) use @usort($a);

2) 使用 @usort($a);

$a = array('id' => 2,'val' => 3, 'ind' => 3);
@usort($a);

回答by chriswoodford

I experienced this problem when PHP was throwing an error within my callback function. So rather than spitting out the actual error that was happening, PHP would throw:

当 PHP 在我的回调函数中抛出错误时,我遇到了这个问题。因此,PHP 不会抛出正在发生的实际错误,而是抛出:

usort(): Array was modified by the user comparison function

usort():数组被用户比较函数修改

回答by user3740692

I found that using PHP5.4 , logging with error_log($message, $message_type, $destination, $extra_headers)causes this error , when I clean log entries my problem solved. Logging may temporarily be suspended by disabling and restoring logging after sort function.

我发现使用 PHP5.4 进行日志记录error_log($message, $message_type, $destination, $extra_headers)会导致此错误,当我清理日志条目时,问题解决了。可以通过在排序功能后禁用和恢复日志记录来暂时暂停日志记录。

回答by timdev

What version of PHP is on the linux box?

linux box上的PHP是什么版本?

Are the error_reporting levels the same on both boxes? Try setting them both to E_ALL.

两个盒子上的 error_reporting 级别是否相同?尝试将它们都设置为 E_ALL。

The warning is almost certainly not lying. It's saying that the comparison function you're passing to usort() is changing the array that you're trying to sort- that could definitely make usort take a long time, possibly forever!

警告几乎肯定不是在说谎。这是说您传递给 usort() 的比较函数正在更改您尝试排序的数组- 这肯定会使 usort 花费很长时间,甚至可能永远!

My first step would be to study the comparison function, and figure out why that's happening. It's possible that if the linux boxes are using a pre-5.3 version, there is some difference in the behavior of some language function used in the comparison function.

我的第一步是研究比较函数,并找出发生这种情况的原因。如果 linux box 使用的是 5.3 之前的版本,则比较函数中使用的某些语言函数的行为可能会有所不同。