簡體   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