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

数组结构是这样的:

clipboard.png

然后组装成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;
}

但是实际上输出的结果是错误的:

clipboard.png

自己猜想,一定是自己的递归逻辑有问题,但是实在是想不明白问题到底出在哪里。所以还请帮忙看下,那段递归代码有什么问题

另外贴出完整的测试代码:

<?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);
阅读 3.6k
2 个回答
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></li>";
        if (isset($value['childs']) && is_array($value['childs'])) {
            creatHtmlTree($value['childs']);
        } 
    }
    $htmlTree .= "</ul>";
    return $htmlTree;
}

解决了。
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;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏