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

4

47

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 loads 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.

Dogmatize answered 13/7, 2010 at 8:29 Comment(0)
B
89

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.

Baptist answered 11/6, 2012 at 18:23 Comment(4)
Let's vote for that bug to be fixed people. bugs.php.net/bug.php?id=50688Saccharoid
I would just add to this, that you do not have to throw an Exception. Simply creating it using new, will also trigger the warning.Simonesimoneau
Does sprintf triggers this as well? This error appears on Wordpress 4.0 admin dashboard... The file referenced is wp-includes\class-wp-theme.php on line 1208 and the function that uses the uasort is called by wp-admin\includes\theme.php and 2 other files once each... All of them have sprintfs but no print_r/var_dump/debug stuff or throws. For now I used @uasort for ignoring as you suggested and it doesn't screw up the dashboard any more.Kosak
Apparently, this is “fixed in PHP 7”.Cadel
S
8

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);
Singlehearted answered 6/3, 2014 at 16:58 Comment(0)
H
1

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.

Hazel answered 13/7, 2010 at 8:37 Comment(2)
It is the same version of PHP on both servers, PHP 5.3.2. I will check the comparison functions.Dogmatize
If the versions are the same, I'd be very keen on making sure the code is actually the same, and any inputs are the same. But looking at whatever line the warning points you to is a good place to start. You might want to paste your comparison function and some example data into your question if you don't find the issue yourself.Hazel
T
1

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.

Triciatrick answered 14/6, 2014 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.