Usort (): Array has been changed by user comparison function

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

usort (): array has been changed by user comparison function

every time a page loads for the first time (it requires the page to loop and load, on Linux servers the page loads in 1 second).

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

+45
php usort
Jul 13 '10 at 8:29
source share
5 answers

There is a PHP error that may trigger this warning even if you do not modify the array.

The short version, if any PHP debugging functions examine the sorting array, they will change the number of links and the usort() trick, thinking that you have changed the data.

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

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

Error https://bugs.php.net/bug.php?id=50688 It affects all versions of PHP> = 5.2.11 (including 5.3. * And probably 5. * and 6).

As of January 2015, it is still open.

As far as I can tell, the only workaround is to either "not do this" (which is pretty difficult for exceptions), or use the @usort() error suppression operator to ignore all errors.

+84
Jun 11 '12 at 18:23
source share

To fix this problem, we can contact 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); 
+8
Mar 06 '14 at 16:58
source share

I had this problem when PHP was causing an error in my callback function. Therefore, instead of spitting out the actual error that occurred, PHP will throw:

usort (): array has been changed by user comparison function

+3
Mar 30 '11 at 20:28
source share

What version of PHP is in the linux window?

Are error_reporting levels the same in both cases? Try setting them as E_ALL.

The warning almost certainly does not lie. He says the comparison function you pass to usort () changes the array you are trying to sort - which can definitely make usort a long time, maybe forever!

My first step would be to study the comparison function and find out why this is happening. It is possible that if linux mailboxes use version prior to 5.3, there is some difference in the behavior of some language function used in the comparison function.

+1
Jul 13 '10 at 8:37
source share

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

+1
Jun 14 '14 at 15:48
source share



All Articles