首页  >  文章  >  后端开发  >  为什么递归不起作用? 郁闷了

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

WBOY
WBOY原创
2016-06-23 14:24:58901浏览

递归 遍历 php

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;}

回复讨论(解决方案)

你并没有递归! getTree($v['child']); 都是被注释掉的
getTree 有个返回 return $tree;
如果递归的话,应写作 $tree .= getTree($v['child']);

你并没有递归! getTree($v['child']); 都是被注释掉的
getTree 有个返回 return $tree;
如果递归的话,应写作 $tree .= getTree($v['child']);

非常感谢,注释掉,是没有输出没办法了
写作 $tree .= getTree($v['child']); 汗

我说呢,谢谢

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