Skip to content

Instantly share code, notes, and snippets.

@jslim89
Last active October 27, 2015 02:50
Show Gist options
  • Select an option

  • Save jslim89/34d2770610191f9200c7 to your computer and use it in GitHub Desktop.

Select an option

Save jslim89/34d2770610191f9200c7 to your computer and use it in GitHub Desktop.

Revisions

  1. jslim89 revised this gist Oct 27, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions array_single_to_multi_lvl.php
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    * @access public
    * @return array
    */
    function ginota_menu_array_multi_level($menu_items, $depth = 1) {
    function array_multi_level($menu_items, $depth = 1) {
    $items = array();
    $in_current_depth = true;

    @@ -21,7 +21,7 @@ function ginota_menu_array_multi_level($menu_items, $depth = 1) {
    if ($in_current_depth && $item->level > $depth) {
    $in_current_depth = false;
    $item_left = array_slice($menu_items, $key);
    @$items[$key - 1]->children = ginota_menu_array_multi_level($item_left, $item->level);
    @$items[$key - 1]->children = array_multi_level($item_left, $item->level);
    }
    }

  2. jslim89 revised this gist Aug 4, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions array_single_to_multi_lvl.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    /**
    * Refer: https://stackoverflow.com/questions/14961556/convert-one-dimensional-array-into-a-multi-dimensional-array/14963016#14963016
    *
  3. jslim89 renamed this gist Aug 4, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. jslim89 renamed this gist Aug 4, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. jslim89 created this gist Aug 4, 2014.
    28 changes: 28 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    /**
    * 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 ginota_menu_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 = ginota_menu_array_multi_level($item_left, $item->level);
    }
    }

    return $items;
    }