| 
        php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
  [2015-03-02 14:49 UTC] contact at jubianchi dot fr
 Description: ------------ There is a tiny difference on usort result between php < 7 and php7 (master): items are not ordered the same way between those versions. PHP7 seems to be consistent with HVVM but not with earlier version of PHP :) Test script: --------------- <?php // http://3v4l.org/RYTt9 $array = array( array( 'file' => 'file1', 'line' => 1, 'message' => 'foo' ), array( 'file' => 'file1', 'line' => 1, 'message' => 'bar' ) ); usort($array, function($a, $b) { if ($a['file'] !== $b['file']) { return strcmp($a['file'], $b['file']); } else if ($a['line'] === $b['line']) { return 0; } else { return ($a['line'] < $b['line'] ? -1 : 1); } } ); var_dump($array); Expected result: ---------------- array(2) { [0]=> array(3) { ["file"]=> string(5) "file1" ["line"]=> int(1) ["message"]=> string(3) "bar" } [1]=> array(3) { ["file"]=> string(5) "file1" ["line"]=> int(1) ["message"]=> string(3) "foo" } } Actual result: -------------- array(2) { [0]=> array(3) { ["file"]=> string(5) "file1" ["line"]=> int(1) ["message"]=> string(3) "foo" } [1]=> array(3) { ["file"]=> string(5) "file1" ["line"]=> int(1) ["message"]=> string(3) "bar" } } PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             
             | 
    |||||||||||||||||||||||||||
            
                 
                Copyright © 2001-2025 The PHP GroupAll rights reserved.  | 
        Last updated: Tue Nov 04 00:00:01 2025 UTC | 
@matt: As @aharvey said, PHP has never guaranteed a stable sort for "equal" items. If your array was defined in a different order, or if you merely added or removed an item from it, then the result could be in a different order. There is no reason for pos5 to sort before pos3 besides "that's what happened when the author ran this code in PHP 5". Maybe I'm naive but I'd think the test should verify that each item is <= the next item; that is all that sorting guarantees* and assuming more would be a mistake. The code is a bit too complex for me to suggest an actual solution, but I'm thinking along the lines of: $growth = -99; for each $value in the column { assert that $value['growth'] >= $growth; $growth = $value['growth']; } Or you can guarantee a stable sort by doing a final comparison (ie, when all other comparisons returned equality) on a unique, even if arbitrary, value. Perhaps "label" would fit the bill? * given a consistent comparison function