登录

递归

PHP笔记
0 1074

    /**
     * 递归
     * $id 会员ID
     */
    public function recursionSix($id){
        $user = Db::table('表名')->field('id pid username')->where(array('id' => $id))->select();
        $data_down = $this->recursionSixDown($user 0); // 找下级
        $data_up = $this->recursionSixUp($user 0); // 找上级
        dump($data_up);
    }

    /**
     * 递归过程 找下级
     * $user 用户信息
     * $nums 循环次数 $nums - 2 递归找到几级下级(包含自己)
     */
    public function recursionSixDown($user $nums){
        if($nums > 4){
            return $user;
        }
        foreach ($user as $k => $v) {
            $arr = Db::table('表名')->field('id pid username')->where(array('pid' => $v['id']))->select();
            $user[$k]['down'] = $this->recursionSixDown($arr ++$nums);
        }
        return $user;
    }

    /**
     * 递归过程 找上级
     * $user 用户信息
     * $nums 循环次数 $nums - 2 递归找到几级上级(包含自己)
     */
    public function recursionSixUp($user $nums){
        if($nums > 4){
            return $user;
        }
        foreach ($user as $k => $v) {
            $arr = Db::table('表名')->field('id pid username')->where(array('id' => $v['pid']))->select();
            $user[$k]['up'] = $this->recursionSixUp($arr ++$nums);
        }
        return $user;
    }

发表评论

0 个回复