Skip to content

Instantly share code, notes, and snippets.

@jslim89
Last active October 27, 2015 02:50
Show Gist options
  • Save jslim89/34d2770610191f9200c7 to your computer and use it in GitHub Desktop.
Save jslim89/34d2770610191f9200c7 to your computer and use it in GitHub Desktop.
Joomla menu list from single-dimention to multi-dimentional array
<?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