开发者

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

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:

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

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 load开发者_运维技巧s in 1 second).

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.


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

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.

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

  • calling var_dump or print_r on any of the sort data
  • calling debug_backtrace()
  • throwing an exception -- any exception -- or even just creating an exception

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

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.


To resolve this issue we can handle as below

1) use error_reporting

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

2) use @usort($a);

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


What version of PHP is on the linux box?

Are the error_reporting levels the same on both boxes? Try setting them both to 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!

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜