为什么递归不起作用? 郁闷了
程序员文章站
2024-04-04 17:08:11
...
递归 遍历 php
'.$v['catname'].""; if(is_array($v['child'])){ $tree .= ' '; } return $tree;}
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 .= '
- '; //遍历child子数组 foreach($v['child'] as $sv){ $tree .= '
-
'.$sv['catname'].""; // getTree($v['child']); if(is_array($sv['child'])) { $tree .= '
- '; foreach($sv['child'] as $ssv){ $tree .= '
- '.$ssv['catname']." "; } $tree .= "
"; }// print_r($v['child']);// getTree($v['child']); $tree .= "
回复讨论(解决方案)
你并没有递归! getTree($v['child']); 都是被注释掉的
getTree 有个返回 return $tree;
如果递归的话,应写作 $tree .= getTree($v['child']);
你并没有递归! getTree($v['child']); 都是被注释掉的
getTree 有个返回 return $tree;
如果递归的话,应写作 $tree .= getTree($v['child']);
非常感谢,注释掉,是没有输出没办法了
写作 $tree .= getTree($v['child']); 汗
我说呢,谢谢