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' => [] ] ] ] ]; $name = null; usort($array, $f = function(&$x, &$y) use (&$f, $name){ $myName = $name; $name = $x['name']; usort($x['children'], $f); $name = $y['name']; usort($y['children'], $f); var_dump($myName, $x['children']); return strcasecmp($x['name'], $y['name']); }); var_dump($array);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Warning: {closure}(): Argument #1 ($x) must be passed by reference, value given in /in/hLINu on line 38 Warning: {closure}(): Argument #2 ($y) must be passed by reference, value given in /in/hLINu on line 38 Warning: {closure}(): Argument #1 ($x) must be passed by reference, value given in /in/hLINu on line 42 Warning: {closure}(): Argument #2 ($y) must be passed by reference, value given in /in/hLINu on line 42 NULL array(0) { } Warning: {closure}(): Argument #1 ($x) must be passed by reference, value given in /in/hLINu on line 44 Warning: {closure}(): Argument #2 ($y) must be passed by reference, value given in /in/hLINu on line 44 NULL array(0) { } Warning: {closure}(): Argument #1 ($x) must be passed by reference, value given in /in/hLINu on line 44 Warning: {closure}(): Argument #2 ($y) must be passed by reference, value given in /in/hLINu on line 44 NULL array(0) { } Warning: {closure}(): Argument #1 ($x) must be passed by reference, value given in /in/hLINu on line 44 Warning: {closure}(): Argument #2 ($y) must be passed by reference, value given in /in/hLINu on line 44 NULL array(0) { } NULL 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 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Warning: {closure}(): Argument #1 ($x) must be passed by reference, value given in /in/hLINu on line 38 Warning: {closure}(): Argument #2 ($y) must be passed by reference, value given in /in/hLINu on line 38 Warning: {closure}(): Argument #1 ($x) must be passed by reference, value given in /in/hLINu on line 42 Warning: {closure}(): Argument #2 ($y) must be passed by reference, value given in /in/hLINu on line 42 NULL array(0) { } Warning: {closure}(): Argument #1 ($x) must be passed by reference, value given in /in/hLINu on line 44 Warning: {closure}(): Argument #2 ($y) must be passed by reference, value given in /in/hLINu on line 44 NULL array(0) { } Warning: {closure}(): Argument #1 ($x) must be passed by reference, value given in /in/hLINu on line 44 Warning: {closure}(): Argument #2 ($y) must be passed by reference, value given in /in/hLINu on line 44 NULL array(0) { } Warning: {closure}(): Argument #1 ($x) must be passed by reference, value given in /in/hLINu on line 44 Warning: {closure}(): Argument #2 ($y) must be passed by reference, value given in /in/hLINu on line 44 NULL array(0) { } NULL 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.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33
NULL array(0) { } NULL array(0) { } NULL array(0) { } NULL array(0) { } NULL 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.24 - 5.5.35, 5.6.8 - 5.6.21
NULL array(0) { } NULL array(0) { } NULL array(0) { } NULL array(0) { } NULL array(0) { } NULL 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:
168.75 ms | 404 KiB | 181 Q