首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

为何递归不起作用? 郁闷了

2013-10-27 
为什么递归不起作用? 郁闷了Array([0] Array([0] 1[catid] 1[1] 0[parentid] 0[2] 关于

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


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

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

热点排行