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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?