3v4l.org

run code in 300+ PHP versions simultaneously
<?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 $result = array(); var_dump($result == $target); */ $items = array( array( 'mid' => 1, // Menuid 'pid' => 3, // parentid 'title' => 'Wurm 1.1', // title 'level' => 2, // level 'sid' => 10 // sortid ), array( 'mid' => 2, // Menuid 'pid' => 6, // parentid 'title' => 'Vogel 2.1', // title 'level' => 2, // level 'sid' => 30 // sortid ), array( 'mid' => 3, // Menuid 'pid' => 0, // parentid 'title' => 'Tiger 1', // title 'level' => 1, // level 'sid' => 10 // sortid ), array( 'mid' => 4, // Menuid 'pid' => 6, // parentid 'title' => 'Hund 2.2', // title 'level' => 2, // level 'sid' => 40 // sortid ), array( 'mid' => 5, // Menuid 'pid' => 3, // parentid 'title' => 'Katze 1.2', // title 'level' => 2, // level 'sid' => 11 // sortid ), array( 'mid' => 6, // Menuid 'pid' => 0, // parentid 'title' => 'Pferd 2', // title 'level' => 1, // level 'sid' => 20 // sortid ), array( 'mid' => 7, // Menuid 'pid' => 1, // parentid 'title' => 'Baer 1.1.1', // title 'level' => 3, // level 'sid' => 0 // sortid ), array( 'mid' => 8, // Menuid 'pid' => 3, // parentid 'title' => 'Schwein 1.3', // title 'level' => 2, // level 'sid' => 12 // sortid ), array( 'mid' => 9, // Menuid 'pid' => 4, // parentid 'title' => 'Esel 2.2.1', // title 'level' => 3, // level 'sid' => 0 // sortid ) ); function listItems($arr=array(), $pid=0, $level=1) { $result=''; foreach ($arr as $key => $item) { if (!in_array($pid, $item)) continue; if (($item['pid'] == $pid) && ($item['level'] == $level)) { $result .= $item['title'] . '>>' . listItems($arr, $item['mid'], ($level+1)); } } return $result; } listItems($items); /* foreach($items as $item){ } function array_msort($array, $cols) { $colarr = array(); foreach ($cols as $col => $order) { $colarr[$col] = array(); foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); } } $eval = 'array_multisort('; foreach ($cols as $col => $order) { $eval .= '$colarr[\''.$col.'\'],'.$order.','; } $eval = substr($eval,0,-1).');'; eval($eval); $ret = array(); foreach ($colarr as $col => $arr) { foreach ($arr as $k => $v) { $k = substr($k,1); if (!isset($ret[$k])) $ret[$k] = $array[$k]; $ret[$k][$col] = $array[$k][$col]; } } return $ret; } function listItems($items, $level=0, $child=false, $pid=0) { array_msort($items, array(4 => SORT_DESC)); $rest = ''; foreach($items as $item) { if ($item[3] != $level) { continue; } if ($child == false) { $rest .= '<li>'. $item[2] .'<ul>'. listItems($items, $level+1, true, $item[0]) .'</ul></li>'; } else { if ($pid == $item[1]) { $rest .= '<li>'. $item[2] .'<ul>'. listItems($items, $level+1, true, $item[0]) .'</ul></li>'; } } } return '<ul>' . $rest . '</ul>'; } echo listItems($navitems);*/

preferences:
36.16 ms | 402 KiB | 5 Q