3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $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 usort($menu, function($a,$b) { return $a[3] == $b[3] ? ($a[4] < $b[4] ? -1 : 1) : ($a[3] < $b[3] ? -1 : 1); }); $cache = array(); /* foreach ($menu as $m) { if (!isset($cache[$m[1]])) { $cache[$m[1]] = array(); } $cache[$m[1]][] = $m; } function m($m, $l, $r) { foreach($m[$l] as $e) { $r[] = $e; if (isset($m[$e[0]])) { $r = m($m, $e[0], $r); } } return $r; } */ var_dump($menu); $result = $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);
based on MPUL9
Output for 7.0.0 - 7.2.0
array(9) { [0]=> array(5) { [0]=> int(3) [1]=> int(0) [2]=> string(7) "Tiger 1" [3]=> int(1) [4]=> int(10) } [1]=> array(5) { [0]=> int(6) [1]=> int(0) [2]=> string(7) "Pferd 2" [3]=> int(1) [4]=> int(20) } [2]=> array(5) { [0]=> int(1) [1]=> int(3) [2]=> string(8) "Wurm 1.1" [3]=> int(2) [4]=> int(10) } [3]=> array(5) { [0]=> int(5) [1]=> int(3) [2]=> string(9) "Katze 1.2" [3]=> int(2) [4]=> int(11) } [4]=> array(5) { [0]=> int(8) [1]=> int(3) [2]=> string(11) "Schwein 1.3" [3]=> int(2) [4]=> int(12) } [5]=> array(5) { [0]=> int(2) [1]=> int(6) [2]=> string(9) "Vogel 2.1" [3]=> int(2) [4]=> int(30) } [6]=> array(5) { [0]=> int(4) [1]=> int(6) [2]=> string(8) "Hund 2.2" [3]=> int(2) [4]=> int(40) } [7]=> array(5) { [0]=> int(9) [1]=> int(4) [2]=> string(10) "Esel 2.2.1" [3]=> int(3) [4]=> int(0) } [8]=> array(5) { [0]=> int(7) [1]=> int(1) [2]=> string(10) "Baer 1.1.1" [3]=> int(3) [4]=> int(0) } } bool(false)
Output for 5.3.19 - 5.6.28
array(9) { [0]=> array(5) { [0]=> int(3) [1]=> int(0) [2]=> string(7) "Tiger 1" [3]=> int(1) [4]=> int(10) } [1]=> array(5) { [0]=> int(6) [1]=> int(0) [2]=> string(7) "Pferd 2" [3]=> int(1) [4]=> int(20) } [2]=> array(5) { [0]=> int(1) [1]=> int(3) [2]=> string(8) "Wurm 1.1" [3]=> int(2) [4]=> int(10) } [3]=> array(5) { [0]=> int(5) [1]=> int(3) [2]=> string(9) "Katze 1.2" [3]=> int(2) [4]=> int(11) } [4]=> array(5) { [0]=> int(8) [1]=> int(3) [2]=> string(11) "Schwein 1.3" [3]=> int(2) [4]=> int(12) } [5]=> array(5) { [0]=> int(2) [1]=> int(6) [2]=> string(9) "Vogel 2.1" [3]=> int(2) [4]=> int(30) } [6]=> array(5) { [0]=> int(4) [1]=> int(6) [2]=> string(8) "Hund 2.2" [3]=> int(2) [4]=> int(40) } [7]=> array(5) { [0]=> int(7) [1]=> int(1) [2]=> string(10) "Baer 1.1.1" [3]=> int(3) [4]=> int(0) } [8]=> array(5) { [0]=> int(9) [1]=> int(4) [2]=> string(10) "Esel 2.2.1" [3]=> int(3) [4]=> int(0) } } bool(false)