php tree ul li hierarchy menu from array

孤街浪徒 提交于 2019-12-11 09:18:13

问题


we have this array from mysqli query output :

$items = Array
(
    Array
    (
        'id' => 1,
        'title' => 'menu1',
        'parent_id' => 0
    ),
    Array
    (
        'id' => 2,
        'title' => 'submenu1-1',
        'parent_id' => 1
    ),
    Array
    (
        'id' => 3,
        'title' => 'submenu1-2',
        'parent_id' => 1
    ),
    Array
    (
        'id' => 4,
        'title' => 'menu2',
        'parent_id' => 0
    ),
    Array
    (
        'id' => 5,
        'title' => 'submenu2-1',
        'parent_id' => 4
    )
);

and we need this html output with php :

<ul>
        <li><a>menu1</a>
             <ul>
                 <li><a>submenu1-1</a></li>
                 <li><a>submenu1-2</a></li>
             </ul>
        </li>
        <li><a>menu2</a>
             <ul>
                 <li><a>submenu2-1</a></li>
             </ul>
        </li>
</ul>

can anyone help me ? Probably this is very easy but I have tried everything already without success !!


回答1:


//index elements by id
foreach ($items as $item) {
    $item['subs'] = array();
    $indexedItems[$item['id']] = (object) $item;
}


//assign to parent
$topLevel = array();
foreach ($indexedItems as $item) {
    if ($item->parent_id == 0) {
        $topLevel[] = $item;
    } else {
        $indexedItems[$item->parent_id]->subs[] = $item;
    }
}

//recursive function
function renderMenu($items) {
    $render = '<ul>';

    foreach ($items as $item) {
        $render .= '<li>' . $item->title;
        if (!empty($item->subs)) {
            $render .= renderMenu($item->subs);
        }
        $render .= '</li>';
    }

    return $render . '</ul>';
}

echo renderMenu($topLevel);



回答2:


finally i found answer like this:

function generateTreeMenu($datas, $parent = 0, $limit=0){
            if($limit > 1000) return ''; 
            $tree = '';
            $tree = '<ul>';
            for($i=0, $ni=count($datas); $i < $ni; $i++){
                if($datas[$i]['parent_id'] == $parent){
                    $tree .= '<li><a>';
                    $tree .= $datas[$i]['title'].'</a>';
                    $tree .= generatePageTree($datas, $datas[$i]['id'], $limit++);
                    $tree .= '</li>';
                }
            }
            $tree .= '</ul>';
            return $tree;
}

echo generateTreeMenu($items);



回答3:


The problem here is just the structure of the array, so first you can convert the array to a more suitable structure, then you can draw your list easily.

Here is a function to convert the array:

function makeTree( $rst, $level, &$tree )
{
    for ( $i=0, $n=count($rst); $i < $n; $i++ )
    {
      if ( $rst[$i]['parent_id'] == $level )
      {
        $branch = array(
          'id' => $rst[$i]['id'],
          'title' => $rst[$i]['title'],
          'children' => array()
        );
        makeTree( $rst, $rst[$i]['id'], $branch['children'] );
        $tree[] = $branch;
      }
    }
}

Mode of use:

$tree = array();
makeTree( $originalArray, 0, $tree );

At the end, you will have a new array in $tree structured as shown below, which you can easily draw in your view.

Array
(
    [0] => Array
        (
            [id] => 1
            [title] => menu1
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [title] => submenu1-1
                            [children] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 3
                            [title] => submenu1-2
                            [children] => Array
                                (
                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 4
            [title] => menu2
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [title] => submenu2-1
                            [children] => Array
                                (
                                )

                        )

                )

        )

)



回答4:


Try this

$node = array();
foreach ($items as $item) {
    if ($item['parent_id'] == 0) {
        $node[$item['id']][$item['id']] = $item['title'];
    } else {
        $node[$item['parent_id']][$item['id']] = $item['title'];
    }
}
$result = array();
foreach ($node as $key => $value) {
    $result[$value[$key]] = array_diff($value, array($key => $value[$key]));
}


来源:https://stackoverflow.com/questions/26003141/php-tree-ul-li-hierarchy-menu-from-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!