php遞歸無限查詢上級或者下級


   $this->get_array($user['uid'],1);

    function get_array($user_id,$top=0){
        $sql = 'SELECT * FROM  ' . tablename('weixinmao_house_userinfo').'where pid=:pid';
        $params = array(':pid' => $user_id);
        $rows = pdo_fetchall($sql, $params);
        foreach ($rows as $key=>$value)
        {
            $r = $this->get_array($value['uid']); //調用函數,傳入參數,繼續查詢下級
            $arr[0]['children'][$key]['uid']= $value['uid']; //組合數組
            $arr[0]['children'][$key]['wechaname']= $value['wechaname']; //組合數組
            if(is_array($r)){
                $arr[0]['children'][$key]['children']= $r[0]['children'];
            }

        }
        return $arr;
    }

 

 

方法二:

function get_arrays($user_id,$arr = array()){
    $sql = 'SELECT * FROM  ' . tablename('weixinmao_house_userinfo').'where uid=:uid';
    $params = array(':uid' => $user_id);
    $rows = pdo_fetch($sql, $params);

    $arr = array_push($arr,$rows);
    if (empty($arr['pid'])){
        return $arr;
    }
    $this->get_arrays($rows['pid'],$arr);

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM