3v4l.org

run code in 300+ PHP versions simultaneously
<?php $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); var_dump($x['name'], $x['children']); return strcasecmp($x['name'], $y['name']); }); var_dump($array);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Warning: {closure}(): Argument #1 ($x) must be passed by reference, value given in /in/12ICb on line 36 Warning: {closure}(): Argument #2 ($y) must be passed by reference, value given in /in/12ICb on line 36 Warning: {closure}(): Argument #1 ($x) must be passed by reference, value given in /in/12ICb on line 38 Warning: {closure}(): Argument #2 ($y) must be passed by reference, value given in /in/12ICb on line 38 string(1) "B" array(0) { } Warning: {closure}(): Argument #1 ($x) must be passed by reference, value given in /in/12ICb on line 39 Warning: {closure}(): Argument #2 ($y) must be passed by reference, value given in /in/12ICb on line 39 string(1) "Z" array(0) { } Warning: {closure}(): Argument #1 ($x) must be passed by reference, value given in /in/12ICb on line 39 Warning: {closure}(): Argument #2 ($y) must be passed by reference, value given in /in/12ICb on line 39 string(1) "Y" array(0) { } Warning: {closure}(): Argument #1 ($x) must be passed by reference, value given in /in/12ICb on line 39 Warning: {closure}(): Argument #2 ($y) must be passed by reference, value given in /in/12ICb on line 39 string(1) "Z" array(0) { } string(3) "Foo" array(2) { [0]=> array(2) { ["name"]=> string(1) "A" ["children"]=> array(0) { } } [1]=> array(2) { ["name"]=> string(1) "B" ["children"]=> array(0) { } } } 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) { } } } } }
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
string(1) "B" array(0) { } string(1) "Z" array(0) { } string(1) "Y" array(0) { } string(1) "Z" array(0) { } string(3) "Foo" array(2) { [0]=> array(2) { ["name"]=> string(1) "A" ["children"]=> array(0) { } } [1]=> array(2) { ["name"]=> string(1) "B" ["children"]=> array(0) { } } } 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) { } } } } }
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
string(1) "X" array(0) { } string(1) "Y" array(0) { } string(1) "Z" array(0) { } string(1) "Y" array(0) { } string(1) "A" array(0) { } string(3) "Bar" 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) { } } } 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) { } } } } }

preferences:
238.91 ms | 404 KiB | 326 Q