欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP 递归兑现层级树状展现数据

程序员文章站 2022-04-21 15:49:28
...
PHP 递归实现层级树状展现数据

?

 $arr['id'],         'fid' => $arr['fid'],        'name' => $arr['name'],     );}// 将数据按照缩进简单排列 见图1function 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 '
-----------------------------------
';// 将数据按照所属关系封装 见图2function 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再次展现 见图3function tree2html($tree) { echo '
    '; foreach($tree as $leaf) { echo '
  • ' .$leaf['name']; if(! empty($leaf['children'])) tree2html($leaf['children']); echo '
  • '; } echo '
';}tree2html($tree);

?

?

?

图 1:
PHP 递归兑现层级树状展现数据

?图 2:


PHP 递归兑现层级树状展现数据
??图 3:


PHP 递归兑现层级树状展现数据

PHP 递归兑现层级树状展现数据

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频