当前位置: 代码迷 >> PHP >> php树形结构展示
  详细解决方案

php树形结构展示

热度:105   发布时间:2016-04-28 17:55:45.0
php树形结构显示
数组array(array(7,5),array(7,6,1),array(7,6,2,3,5),array(7,6,2,3,6))
在前端想显示成树形,请问如何处理?
------解决思路----------------------
$a = array(array(7,5),array(7,6,1),array(7,6,2,3,5),array(7,6,2,3,6));

$root = array();
foreach($a as $item) {
  $p =& $root;
  foreach($item as $k) {
    if(!isset($p[$k])) $p[$k] = array();
    $p =& $p[$k];
  }
}
print_r($root);
Array
(
    [7] => Array
        (
            [5] => Array
                (
                )

            [6] => Array
                (
                    [1] => Array
                        (
                        )

                    [2] => Array
                        (
                            [3] => Array
                                (
                                    [5] => Array
                                        (
                                        )

                                    [6] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)

  相关解决方案