热搜:NVER node 开发 php

php树形结构显示

2024-08-13 13:35:01
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                                        (                                        )                                )                        )                )        ))