LOKESH
LOKESH

Reputation: 1301

convert array to treeview in php

How to convert array to treeview in php

Below is my array

Array
(
    [0] => Array
        (
            [efi] => Array
                (
                )
        )
    [1] => Array
        (
            [grub] => Array
                (
                    [0] => Array
                        (
                            [fonts] => Array
                                (
                                )
                        )
                    [1] => Array
                        (
                            [i386-pc] => Array
                                (
                                )
                        )
                    [2] => Array
                        (
                            [locale] => Array
                                (
                                )
                        )
                    [3] => Array
                        (
                            [x86_64-efi] => Array
                                (
                                )
                        )
                )
        )
)

I want output as treeview

and under grub I want

I have used below code for same

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

generateTreeMenu($dir_array);

it show warning Undefined property: stdClass::$parent_id in

Upvotes: 0

Views: 2087

Answers (1)

Nic3500
Nic3500

Reputation: 8611

Here is my solution, based on your post:

<?php
echo "<pre>";
$dir_array = array( array( "efi"  => array() ),
                    array( "grub" => array(
                                           array("fonts"      => array()),
                                           array("i386-pc"    => array()),
                                           array("locale"     => array()),
                                           array("x86_64-efi" => array())
                                          )
                         )
                  );
print_r($dir_array);
echo "</pre>\n";

function mygenerateTreeMenu($dir_array,$limit = 0)
{
    $key = '';
    if ($limit > 1000) return '';
    foreach ($dir_array as $key => $value)
    {
        if (!is_int($key))
        {
            $tree .= "<li>";
            $tree .= "<a>$key</a><ul>";
            $tree .= mygenerateTreeMenu($value,$limit++);
            $tree .= "</ul></li>\n";
        }
        else
        {
            $tree .= mygenerateTreeMenu($value,$limit++);
        }
    }
    return $tree;
}

echo "<ul>\n";
$tree = mygenerateTreeMenu($dir_array);
echo $tree;
echo "</ul>\n";
?>

I removed the $parent as I did not need it. Then because of the way your array is built, integer indexes are ignored. If you can modify your array, you could streamline it and remove integer indexes completely and treat is as a hash table. It would make the recursive code simpler.

Upvotes: 1

Related Questions