为了账号安全,请及时绑定邮箱和手机立即绑定

多维数组分类树 组合html树的问题?(递归)

多维数组分类树 组合html树的问题?(递归)

PHP
桃花长相依 2019-03-12 13:27:11
数组结构是这样的: 然后组装成html结构的方法如下: function creatHtmlTree($tree) { static $htmlTree; $htmlTree .= '<ul>'; foreach ($tree as $key => $value) { $htmlTree .= "<li><span><i class='icon-folder-open'></i>{$value['name']} </span> <a href=''>Goes somewhere</a>"; if (isset($value['childs']) && is_array($value['childs'])) { $html = creatHtmlTree($value['childs']); $htmlTree .= $html; } $htmlTree .= "</li>"; } $htmlTree .= "</ul>"; return $htmlTree; } 但是实际上输出的结果是错误的: 自己猜想,一定是自己的递归逻辑有问题,但是实在是想不明白问题到底出在哪里。所以还请帮忙看下,那段递归代码有什么问题 另外贴出完整的测试代码: <?php $data[] =array('id'=>1,'parentid'=>0,'name'=>'中国'); $data[] =array('id'=>2,'parentid'=>0,'name'=>'美国'); $data[] =array('id'=>3,'parentid'=>0,'name'=>'韩国'); $data[] =array('id'=>4,'parentid'=>1,'name'=>'北京'); $data[] =array('id'=>5,'parentid'=>1,'name'=>'上海'); $data[] =array('id'=>6,'parentid'=>1,'name'=>'广西'); $data[] =array('id'=>7,'parentid'=>6,'name'=>'桂林'); $data[] =array('id'=>8,'parentid'=>6,'name'=>'南宁'); $data[] =array('id'=>9,'parentid'=>6,'name'=>'柳州'); $data[] =array('id'=>10,'parentid'=>2,'name'=>'纽约'); $data[] =array('id'=>11,'parentid'=>2,'name'=>'华盛顿'); $data[] =array('id'=>12,'parentid'=>3,'name'=>'首尔'); /**格式化数组输出**/ function p($arr) { echo "<pre>"; echo '========================开始========================'; echo "</br>"; if( $arr ){ print_r($arr); } else { echo '此值为空'; } echo "</br>"; echo '========================结束========================'; echo "</pre>"; } /** * 多维数组树形结构 */ function tree($data, $pid = 0) { // static $html = ''; // $html .= '<ul>'; $children = []; // p($data); foreach ($data as $key => $value) { if ($value['parentid'] == $pid) { // $html .= '<li>'; $children[] = $value; } } if (empty($children)) { return null; } foreach ($children as $key => $value) { $chid = tree($data, $value['id']); if ($chid != null) { $children[$key]['childs'] = $chid; } } // $html .= '</ul>'; return $children; } function creatHtmlTree($tree) { static $htmlTree; $htmlTree .= '<ul>'; foreach ($tree as $key => $value) { $htmlTree .= "<li><span><i class='icon-folder-open'></i>{$value['name']} </span> <a href=''>Goes somewhere</a>"; if (isset($value['childs']) && is_array($value['childs'])) { $html = creatHtmlTree($value['childs']); $htmlTree .= $html; } $htmlTree .= "</li>"; } $htmlTree .= "</ul>"; return $htmlTree; } $tree = tree($data); $htmlTree = creatHtmlTree($tree); p($tree); p($htmlTree);
查看完整描述

1 回答

?
慕仙森

TA贡献1827条经验 获得超7个赞

解决了。
creatHtmlTree()方法中把static $htmlTree去掉就可以了;

完整版函数:

function creatHtmlTree($tree)
{
    $htmlTree = '<ul>';
    foreach ($tree as $key => $value) {
        $htmlTree .= "<li><span><i class='icon-folder-open'></i>{$value['name']} </span> <a href=''>Goes somewhere</a>";
        if (isset($value['childs']) && is_array($value['childs'])) {
            $html = creatHtmlTree($value['childs']);
            $htmlTree .= $html;
        } 
        $htmlTree .= "</li>";
    }
    $htmlTree .= "</ul>";
    return $htmlTree;
}
查看完整回答
反对 回复 2019-03-18
  • 1 回答
  • 0 关注
  • 532 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信