为什么递归不起作用? 郁闷了

杰克船长的微笑 2013-10-25 11:39:24

Array
(
[0] => Array
(
[0] => 1
[catid] => 1
[1] => 0
[parentid] => 0
[2] => 关于我们
[catname] => 关于我们
[3] => 4
[listorder] => 4
[4] => 0
[items] => 0
[5] => 1
[type] => 1
[child] => Array
(
[0] => Array
(
[0] => 2
[catid] => 2
[1] => 1
[parentid] => 1
[2] => 关于我们
[catname] => 关于我们
[3] => 1
[listorder] => 1
[4] => 0
[items] => 0
[5] => 1
[type] => 1
)

[1] => Array
(
[0] => 3
[catid] => 3
[1] => 1
[parentid] => 1
[2] => 公司荣誉
[catname] => 公司荣誉
[3] => 2
[listorder] => 2
[4] => 0
[items] => 0
[5] => 1
[type] => 1
)

[2] => Array
(
[0] => 4
[catid] => 4
[1] => 1
[parentid] => 1
[2] => 联系我们
[catname] => 联系我们
[3] => 3
[listorder] => 3
[4] => 0
[items] => 0
[5] => 1
[type] => 1
)

[3] => Array
(
[0] => 5
[catid] => 5
[1] => 1
[parentid] => 1
[2] => 加入我们
[catname] => 加入我们
[3] => 4
[listorder] => 4
[4] => 0
[items] => 0
[5] => 1
[type] => 1
)

[4] => Array
(
[0] => 6
[catid] => 6
[1] => 1
[parentid] => 1
[2] => 友情链接
[catname] => 友情链接
[3] => 5
[listorder] => 5
[4] => 0
[items] => 0
[5] => 2
[type] => 2
)

)

)
)


function getTree($arr)
{
// $row = $arr;
foreach($arr as $k=>$v)
{
$tree .= '<li><span><a href="content.php?catid='.$v['catid'].'" target="right">'.$v['catname']."</a></span>";

if(is_array($v['child'])){

$tree .= '<ul>';
//遍历child子数组
foreach($v['child'] as $sv){
$tree .= '<li><span><a href="content.php?catid='.$sv['catid'].'" target="right">'.$sv['catname']."</a></span>";
// getTree($v['child']);

if(is_array($sv['child'])) {
$tree .= '<ul>';
foreach($sv['child'] as $ssv){
$tree .= '<li><a href="content.php?catid='.$ssv['catid'].'" target="right">'.$ssv['catname']."</a></li>";
}
$tree .= "</ul>";
}
$tree .= "</li>";
}
// print_r($v['child']);
// getTree($v['child']);
$tree .= "</ul>";
}

$tree .= '</li>';
}
return $tree;
}
...全文
112 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
你并没有递归! getTree($v['child']); 都是被注释掉的 getTree 有个返回 return $tree; 如果递归的话,应写作 $tree .= getTree($v['child']);
非常感谢,注释掉,是没有输出没办法了 写作 $tree .= getTree($v['child']); 汗 我说呢,谢谢
xuzuning 2013-10-25
  • 打赏
  • 举报
回复
你并没有递归! getTree($v['child']); 都是被注释掉的 getTree 有个返回 return $tree; 如果递归的话,应写作 $tree .= getTree($v['child']);

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧