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

在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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题