递归实现
程序员文章站
2022-05-29 10:18:13
function ad(){ $res=DB::table("category")->get(); $res= json_encode($res); $res= json_decode($res,1); $arr= $this->gettree($res,0); var_dump($arr);} f ......
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;
}