0

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

  • efi

  • grub

and under grub I want

  • fonts

  • i386-pc

  • locale

  • x86_64-efi

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

4
  • 2
    Possible duplicate of php tree ul li hierarchy menu from array Commented Oct 13, 2017 at 12:23
  • @AliAlwash This solution doesn't work for me. Commented Oct 13, 2017 at 12:25
  • I have multidimensional array. Commented Oct 13, 2017 at 12:28
  • Please post generatePageTree(), we cannot replicate your code and try it without it. Commented Oct 13, 2017 at 13:16

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.