PHP无限极查询

//无限级查询上级
function getParent($uid = 0)
{
    if (!$this->find($uid)) {
        return [];
    }
    $pid = $this->where('id', $uid)->value('pid');
    if ($pid) {
        $this->res[] = $pid;
        $this->getParent($pid);
    } else return [];
    return $this->res;
}

//无限级向上查找最近等级的会员信息
function getLevelParent($uid = 0, $level = 0)
{
    if (!$this->find($uid)) {
        return [];
    }
    $parent = $this->where('id', $uid)->find();
    if ($parent['level'] >= $level) {
        return $parent;
    } else {
        return $this->getLevelParent($parent['pid'], $level);
    }
}

//查到无限极代理
function getAgent($user_id)
{
    $user = U::where('id', $user_id)->lock(true)->find();
    if (!$user) {
        return [];
    }
    if ($user->isagent) {
        return $user;
    } else {
        return $this->getAgent($user->pid);
    }
}

//获得伞下所有人
function getChild($id)
{
    $childs = M('member')->where(array('parent_id' => $id))->field('id')->select();;
    if (!$childs) {
        return [];
    } else {
        foreach ($childs as $child) {
            $this->childs[] = $child['id'];
            $this->getChild($child['id']);
        }
    }
    return $this->childs;
}

原文链接:https://blog.csdn.net/kentrl/article/details/107704757