繁体   English   中英

来自数组的递归菜单树

[英]Recursive menu tree from array

我浏览了这个网站和 inte.net 的其他地方,我尝试了很多东西,但无法理解从数组构建分层菜单树的原则。

我有一个这样的数组:

$categories = array (
    0 =>
        array(
            'term_id' => '1413',
            'name' => 'Parent',
            'parent' => '0',
        )
    ,
    19 =>
        array(
            'term_id' => '2743',
            'name' => 'Child',
            'parent' => '1413',
        )
    ,
    21 =>
        array(
            'term_id' => '2744',
            'name' => 'Grand child',
            'parent' => '2743',
        )
    ,
    22 =>
        array(
            'term_id' => '2738',
            'name' => 'Grand Child',
            'parent' => '2716',
        )
    ,
    25 =>
        array(
            'term_id' => '2716',
            'name' => 'Child',
            'parent' => '1413',
        )
    ,
    33 =>
        array(
            'term_id' => '2722',
            'name' => 'Child',
            'parent' => '1413',
        )
    ,
    41 =>
        array(
            'term_id' => '2720',
            'name' => 'Grand child',
            'parent' => '2716',
        )
    ,
    58 =>
        array(
            'term_id' => '2717',
            'name' => 'Grand child',
            'parent' => '2716',
        )
    ,
    618 => 
        array(
            'term_id' => '3321',
            'name' => 'Grand child',
            'parent' => '2743',
        )
    );

我现在想构建一个带有 output 的菜单,例如:

Parent
  - Child
    - Grand child
    - Grand child
- Child
    - Grand child
    - Grand child
Parent
  - Child
    - Grand child
    - Grand child
- Child
    - Grand child
    - Grand child

我得到的最接近的是下面的代码,但我不知道我在做什么。

$hasChild = 0;
$idCount = array();
function buildTree($arr, $parent = 0){
    global $hasChild;
    global $idCount;
    foreach($arr as $item){
        if($item['parent'] == 0 && $hasChild == 0 && !in_array($item['term_id'], $idCount)){
            array_push($idCount, $item['term_id']);
            echo $item['name'];
            $hasChild = 1;
            buildTree($arr, $item['parent']);
        }else{
            if(!in_array($item['term_id'], $idCount)){
                echo '<br>- '.$item['name'];
            }
        }
    }
}

buildTree($categories, 0);

虽然我很高兴我终于得到了一个没有错误的实际 output,但重复的列表对我没有帮助。

我真的希望有人能把我推向正确的方向,因为我只是不知道(我在做什么)......

您可以尝试使用这一方法-如果您也需要缩进,则可以在递归函数中添加一些int参数,该参数定义要添加多少空格作为回显行的前缀...:

function buildTree($arr, $parent = 0, $indent = 0)
{
    foreach($arr as $item)
    {
        if ($item["parent"] == $parent)
        {
            if ($indent != 0)
            {
                echo str_repeat("&nbsp;", $indent) . "-&nbsp;";
            }
            echo $item['name'] . '<br/>';
            buildTree($arr, $item['term_id'], $indent + 2);
        }
    }
}

buildTree($categories);

如何更改此功能以使用标签UL和LI构建输出? 我试过这样的:

function buildTree($arr, $parent = 0)
{
    $this->menu .= "<ul>";
    foreach($arr as $item)
    {
        if ($item["parent"] == $parent)
        {  
            $this->menu .= "<li>".$item['name']."</li>";
            buildTree($arr, $item['term_id']);
        }
    }
$this->menu .= "</ul>";
return $this->menu;
}

buildTree($ categories);

但是我每个都有一些空的<ul></ul>

  • 标记为

     <ul> <li>entry 1</li> <ul></ul> <li>entry 2</li> <ul></ul> </ul> 
  • 我做了一个非常简单的代码

        function buildTree($arr, $term_id= 0)
        {
            echo "<ul>";
            foreach ($arr as $menu) {
                if ($menu['parent'] == $term_id) {
                    echo "<li>${menu['name']}</li>";
                    buildTree($arr, $menu['term_id']);
                }
            }
            echo "</ul>";
        }
    

    暂无
    暂无

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM