PHP递归所有下级

/**
 * 获取用户下属ID集合
 * @param $uid int
 * @param $ids string
 * @author jack
 * @throws string
 * @return string
 */
public function getUnderlingUIds($uid, $ids='')
{
    $back = Db('ns_member','',false)->field('uid')->where('parent_uid',$uid)->select();
    if (!empty($back) && is_array($back)) {
        foreach ($back as $v) {
            //防止当前人的ID重复去查询,形成恶性循环
            if ($v['id'] == $uid) {
                continue;
            }
            $back2 = Db('ns_member','',false)->where('parent_uid',$v['uid'])->count('uid');
            if ($back2 > 0) {
                $ids = $this->getUnderlingUIds($v['uid'],$ids);
            } else {
                $ids .= ','.$v['id'];
            }
        }
     }
    $ids = $uid.','.$ids.',';
    $ids = str_replace(',,', ",", $ids);
    $ids = trim($ids, ',');
    return $ids;
}

 

上一篇:序列感知推荐系统


下一篇:ibatis iterate VS mybatis foreach