Bug #71238 [NEW]: usort pass by reference inconsistency

From: Date: Tue, 29 Dec 2015 16:39:01 +0000
Subject: Bug #71238 [NEW]: usort pass by reference inconsistency
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-198282@lists.php.net to get a copy of this message
From: eugen dot alter at gmail dot com Operating system: any PHP version: 7.0.1 Package: Arrays related Bug Type: Bug Bug description:usort pass by reference inconsistency Description: ------------ In PHP 5.4 ... 5.6 passing compared array elements by reference and then changing them from inside comparison function works properly whilst in PHP7 it does not. In sample script: function tries to sort arrays by key "name" on all nested levels. The nested levels are sorted correctly on PHP 5.4 .. 5.6 but not on PHP7. Test script: --------------- $array = [ [ 'name' => 'Foo', 'children' => [ [ 'name' => 'B', 'children' => [] ], [ 'name' => 'A', 'children' => [] ] ] ], [ 'name' => 'Bar', 'children' => [ [ 'name' => 'Z', 'children' => [] ], [ 'name' => 'X', 'children' => [] ], [ 'name' => 'Y', 'children' => [] ] ] ] ]; usort($array, $f = function(&$x, &$y) use (&$f){ usort($x['children'], $f); usort($y['children'], $f); return strcasecmp($x['name'], $y['name']); }); Expected result: ---------------- array(2) { [0]=> array(2) { ["name"]=> string(3) "Bar" ["children"]=> array(3) { [0]=> array(2) { ["name"]=> string(1) "X" ["children"]=> array(0) { } } [1]=> array(2) { ["name"]=> string(1) "Y" ["children"]=> array(0) { } } [2]=> array(2) { ["name"]=> string(1) "Z" ["children"]=> array(0) { } } } } [1]=> array(2) { ["name"]=> string(3) "Foo" ["children"]=> array(2) { [0]=> array(2) { ["name"]=> string(1) "A" ["children"]=> array(0) { } } [1]=> array(2) { ["name"]=> string(1) "B" ["children"]=> array(0) { } } } } } Actual result: -------------- array(2) { [0]=> array(2) { ["name"]=> string(3) "Bar" ["children"]=> array(3) { [0]=> array(2) { ["name"]=> string(1) "Z" ["children"]=> array(0) { } } [1]=> array(2) { ["name"]=> string(1) "X" ["children"]=> array(0) { } } [2]=> array(2) { ["name"]=> string(1) "Y" ["children"]=> array(0) { } } } } [1]=> array(2) { ["name"]=> string(3) "Foo" ["children"]=> array(2) { [0]=> array(2) { ["name"]=> string(1) "B" ["children"]=> array(0) { } } [1]=> array(2) { ["name"]=> string(1) "A" ["children"]=> array(0) { } } } } } -- Edit bug report at https://bugs.php.net/bug.php?id=71238&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71238&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71238&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71238&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=71238&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=71238&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=71238&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=71238&r=needscript Try newer version: https://bugs.php.net/fix.php?id=71238&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=71238&r=support Expected behavior: https://bugs.php.net/fix.php?id=71238&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=71238&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=71238&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=71238&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71238&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=71238&r=dst IIS Stability: https://bugs.php.net/fix.php?id=71238&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=71238&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=71238&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=71238&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=71238&r=mysqlcfg

« previous php.bugs (#198282) next »