来自数组的php tree ul li层次结构菜单

时间:2014-09-23 19:25:26

标签: php treeview hierarchy menuitem nav

我们有来自mysqli查询输出的这个数组:

$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
    )
);

我们需要这个带有php的html输出:

<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>
谁能帮助我吗? 可能这很容易,但我已经尝试过一切都没有成功!!

4 个答案:

答案 0 :(得分:3)

最后我找到了这样的答案:

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);

答案 1 :(得分: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 :(得分:1)

这里的问题只是数组的结构,所以首先你可以将数组转换为更合适的结构,然后你可以轻松地绘制列表。

这是一个转换数组的函数:

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;
      }
    }
}

使用方式:

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

最后,您将在$tree中创建一个新阵列,如下所示,您可以在视图中轻松绘制。

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
                                (
                                )

                        )

                )

        )

)

答案 3 :(得分:0)

试试这个

$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]));
}