function ad(){ $res=DB::table("category")->get(); $res= json_encode($res); $res= json_decode($res,1); $arr= $this->gettree($res,0); var_dump($arr);} function gettree($data,$pid){ $tree=[]; foreach ($data as $k=>$v){ if ($v['pid']==$pid){ $v['son']=$this->gettree($data,$v['id']); $tree[]=$v; } } return $tree; }