php递归无限分类、根据子类获取所有顶类
1 //递归无限分类树 2 public static function diGui($data, $pid) 3 { 4 $arr = collect([]); 5 if (empty($data)) { 6 return ''; 7 } 8 9 foreach ($data as $key => $value) { 10 if ($value['parent_id'] == $pid) { 11 $value['submenu'] = self::diGui($data, $value['id']); 12 $arr[] = $value; 13 } 14 } 15 return $arr; 16 } 17 18 //根据子类id的所获取有父类 19 public function getParents($data,$id){ 20 $tree=array(); 21 foreach($data as $item){ 22 if($item['id']==$id){ 23 if($item['parent_id']>0) 24 $tree=array_merge($tree,self::getParents($data,$item['parent_id'])); 25 $tree[]=$item; 26 break; 27 } 28 } 29 return $tree; 30 }