时间:2021-07-01 10:21:17 帮助过:14人阅读
?
$arr['id'], 'fid' => $arr['fid'], 'name' => $arr['name'], ); } // 将数据按照缩进简单排列 见图1 function data2arr($tree, $rootId = 0, $level = 0) { foreach($tree as $leaf) { if($leaf['fid'] == $rootId) { echo str_repeat(' ', $level) . $leaf['id'] . ' ' . $leaf['name'] . '
'; foreach($tree as $l) { if($l['fid'] == $leaf['id']) { data2arr($tree, $leaf['id'], $level + 1); break; } } } } } data2arr($data); echo '
-----------------------------------
'; // 将数据按照所属关系封装 见图2 function arr2tree($tree, $rootId = 0) { $return = array(); foreach($tree as $leaf) { if($leaf['fid'] == $rootId) { foreach($tree as $subleaf) { if($subleaf['fid'] == $leaf['id']) { $leaf['children'] = arr2tree($tree, $leaf['id']); break; } } $return[] = $leaf; } } return $return; } $tree = arr2tree($data); print_r($tree); echo '
-----------------------------------
'; // 将数据使用HTML再次展现 见图3 function tree2html($tree) { echo '
?
?
?
图 1:
?图 2:
??图 3: