dongshi7433
dongshi7433
2017-10-13 12:14
浏览 57
已采纳

在php中将数组转换为treeview

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanne9313
    duanne9313 2017-10-13 13:49
    已采纳

    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>
    ";
    
    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>
    ";
            }
            else
            {
                $tree .= mygenerateTreeMenu($value,$limit++);
            }
        }
        return $tree;
    }
    
    echo "<ul>
    ";
    $tree = mygenerateTreeMenu($dir_array);
    echo $tree;
    echo "</ul>
    ";
    ?>
    

    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.

    点赞 评论

相关推荐