php 递归返回值的问题

php 递归返回值的问题

请大佬帮我看看 递归循环 查找上级 在循环里面打印数据全部拿到了,但是return 返回数据后却是空,请问是怎么回事??

    public function for_enter($data=array(),$level=0,$shu=array(),$s=0){
        $ui = M('ui');
        $array = array();
        $where['is_enter'] = 1;
        foreach ($data as $k => $v) {
            $child = $ui->where(array('code'=>$v['pcode']))->select();
            if (empty($child)) {
                return '';
            }else{
                if ($child[0]['level'] == 4) {
                    $this->for_enter($child,$level,'');
                }else{
                    if ($child[0]['is_enter'] == 1) {
                    if (($level - $child[0]['level']) == 3) {
                        $shu['three'] = $child[0];
                        $this->for_enter($child,$level,$shu);
                    }
                    
                    if (($level - $child[0]['level']) == 2) {
                        $shu['tow'] = $child[0];
                        $this->for_enter($child,$level,$shu);
                    }
                    if (($level - $child[0]['level']) == 1) {
                        //判断是几级
                        $shu['one'] = $child[0];
                        $this->for_enter($child,$level,$shu);
                    }
                    }
                }
            }
            $array[] = $shu;
        }
        
        return $array;
    }

打印数据

clipboard.png

clipboard.png

返回数据

clipboard.png

clipboard.png

阅读 2k
2 个回答

其实我看到你说返回值为空我大概就知道问题在哪里了,直接看了一下你递归调用函数的部分。如果没错的话,你第一个应该是直接进入了 $child[0]['level'] == 4 的条件内,返回的直接为默认的 $shu = array()

在这个地方dump两个值就知道了clipboard.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题