Last active
October 27, 2015 02:50
-
-
Save jslim89/34d2770610191f9200c7 to your computer and use it in GitHub Desktop.
Joomla menu list from single-dimention to multi-dimentional array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Refer: https://stackoverflow.com/questions/14961556/convert-one-dimensional-array-into-a-multi-dimensional-array/14963016#14963016 | |
* | |
* @param mixed $menu_items | |
* @param int $depth | |
* @access public | |
* @return array | |
*/ | |
function array_multi_level($menu_items, $depth = 1) { | |
$items = array(); | |
$in_current_depth = true; | |
foreach ($menu_items as $key => $item) { | |
if ($item->level < $depth) return $items; | |
else if ($item->level == $depth) { | |
$in_current_depth = true; | |
$items[] = $item; | |
} | |
if ($in_current_depth && $item->level > $depth) { | |
$in_current_depth = false; | |
$item_left = array_slice($menu_items, $key); | |
@$items[$key - 1]->children = array_multi_level($item_left, $item->level); | |
} | |
} | |
return $items; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment