3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php const IDX_ID = 0; const IDX_PARENT = 1; const IDX_SORT = 4; $menu = array( array(1,3,'Wurm 1.1', 2, 10), array(2,6,'Vogel 2.1', 2, 30), array(3,0,'Tiger 1', 1, 10), array(4,6,'Hund 2.2', 2, 40), array(5,3,'Katze 1.2', 2, 11), array(6,0,'Pferd 2', 1, 20), array(7,1,'Baer 1.1.1', 3, 0), array(8,3,'Schwein 1.3', 2, 12), array(9,4,'Esel 2.2.1', 3, 0), ); // Algorithmus hier function sortMenu($menu, $parentId = 0) { $items = array_filter($menu, function($item) use ($parentId) { return $item[IDX_PARENT] == $parentId; }); if (0 == count($items)) { return array(); } usort($items, function(array $a, array $b) { if ($a[IDX_SORT] == $b[IDX_SORT]) { return 0; } return $a[IDX_SORT] > $b[IDX_SORT] ? 1 : -1; }); $result = array(); foreach ($items as $item) { $result[] = $item; foreach (sortMenu($menu, $item[IDX_ID]) as $child) { $result[] = $child; } } return $result; } $result = sortMenu($menu); $target = array( array(3,0,'Tiger 1', 1, 10), array(1,3,'Wurm 1.1', 2, 10), array(7,1,'Baer 1.1.1', 3, 0), array(5,3,'Katze 1.2', 2, 11), array(8,3,'Schwein 1.3', 2, 12), array(6,0,'Pferd 2', 1, 20), array(2,6,'Vogel 2.1', 2, 30), array(4,6,'Hund 2.2', 2, 40), array(9,4,'Esel 2.2.1', 3, 0), ); var_dump($result == $target);
Output for 5.3.0 - 5.6.30, hhvm-3.10.1 - 3.22.0, 7.0.0 - 7.2.9
bool(true)
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_CONST in /in/qPDZW on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_CONST in /in/qPDZW on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/qPDZW on line 3
Process exited with code 255.