Bug #71238 [Com]: usort pass by reference inconsistency
| From: | levim@php.net | Date: | Tue, 29 Dec 2015 18:24:52 +0000 |
| Subject: | Bug #71238 [Com]: usort pass by reference inconsistency | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-198284@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=71238&edit=1
ID: 71238
Comment by: levim@php.net
Reported by: eugen dot alter at gmail dot com
Summary: usort pass by reference inconsistency
Status: Open
Type: Bug
Package: Arrays related
Operating System: any
PHP Version: 7.0.1
Block user comment: N
Private report: N
New Comment:
Using var_export in PHP 5.5:
array (
0 =>
array (
'name' => 'Bar',
'children' =>
array (
0 =>
array (
'name' => 'X',
'children' =>
array (
),
),
1 =>
array (
'name' => 'Y',
'children' =>
array (
),
),
2 =>
array (
'name' => 'Z',
'children' =>
array (
),
),
),
),
1 =>
array (
'name' => 'Foo',
'children' =>
array (
0 =>
array (
'name' => 'A',
'children' =>
array (
),
),
1 =>
array (
'name' => 'B',
'children' =>
array (
),
),
),
),
)
Using var_export in a slightly outdated master branch:
array (
0 =>
array (
'name' => 'Bar',
'children' =>
array (
0 =>
array (
'name' => 'Z',
'children' =>
array (
),
),
1 =>
array (
'name' => 'X',
'children' =>
array (
),
),
2 =>
array (
'name' => 'Y',
'children' =>
array (
),
),
),
),
1 =>
array (
'name' => 'Foo',
'children' =>
array (
0 =>
array (
'name' => 'B',
'children' =>
array (
),
),
1 =>
array (
'name' => 'A',
'children' =>
array (
),
),
),
),
)
Previous Comments:
------------------------------------------------------------------------
[2015-12-29 16:38:59] eugen dot alter at gmail dot com
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 this bug report at https://bugs.php.net/bug.php?id=71238&edit=1