Maurice
Maurice

Reputation: 1092

Recursive menu tree from array

I've looked around on this site, elsewhere on the internet and i have tried quite some things, but can't get my head around the principle of building an hierarchical menu tree from an array.

I have an array like this:

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

I now want to build a menu with an output like:

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

The closest i get is with the code below, but i just have no clue what i'm doing.

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

Allthough i'm happy i finally get an actual output without errors, the list with duplicates doesn't help me.

I'm really hoping someone can push me in the right direction because i just have no clue (what i'm doing)...

Upvotes: 0

Views: 4739

Answers (3)

Ainul Rokhman
Ainul Rokhman

Reputation: 1

I made a very simple code

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

Upvotes: 0

Wraith
Wraith

Reputation: 501

How change this function to build output with tags UL and LI ? i tried like this:

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

But i have some empty <ul></ul> every

  • tag like

    <ul>
        <li>entry 1</li>
        <ul></ul>
        <li>entry 2</li>
        <ul></ul>
    </ul>
    

    Upvotes: 0

  • ejuhjav
    ejuhjav

    Reputation: 2710

    You can try with this one - if you want the indentations as well you can just add some int parameter to the recursive function that defines how many spaces to add as prefix to the echoed lines...:

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

    Upvotes: 1

    Related Questions