1. 程式人生 > >php當資料量不是很大的時候,使用【遞迴迴圈出樹形結構】的一個使用者下面的所有人

php當資料量不是很大的時候,使用【遞迴迴圈出樹形結構】的一個使用者下面的所有人

前提是資料量不大的時候

1.一張表如下:


2.我們要通過遞迴實現樹形結構的一個人下面的所有人都顯示出來:

<span style="font-size:18px;"><strong>    public function test(){</strong></span>
<span style="font-size:18px;"><strong><span style="white-space:pre">	</span>$name="a111111";//找出這個使用者下面的所有人
        $arr=$this->test2($name);
        show_bug($arr);//自己定義的函式
        $this->display();
    }
    //遞迴函式
    public function test2($name,$result=array()){
        global $result;
        $list=M()->table('ot_user')->where(array('UE_accName'=>$name))->select();
        //show_bug($list);die;
        if (!empty($list)) {
            foreach($list as $key=>$val){
                $result[]=$val['ue_account'];
                $this->test2($val['ue_account'],$result);
            }
        }
        return $result;
    }</strong></span>