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

[email protected] ("[email protected]")
Newsgroups php.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=71238&edit=1

 ID:                 71238
 Comment by:         [email protected]
 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.