dongshi7433
dongshi7433
2017-10-13 12:14

在php中将数组转换为treeview

  • php
  • arrays
  • 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条回答

为你推荐