重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
1,'name'=>'HeNan','parent_id'=>0), array('id'=>2,'name'=>'nanyang','parent_id'=>1), array('id'=>3,'name'=>'tanghe','parent_id'=>2), array('id'=>4,'name'=>'HuBei','parent_id'=>0), array('id'=>5,'name'=>'wuhan','parent_id'=>4), array('id'=>6,'name'=>'FuJian','parent_id'=>0), array('id'=>7,'name'=>'xiamen','parent_id'=>6), array('id'=>8,'name'=>'huangshi','parent_id'=>5) ); //递归法找指定栏目的子孙树 function subtree($data,$id=0,$lev=1){ $arr = array(); foreach($data as $v){ if($v['parent_id']==$id){ $v['lev'] = $lev; $arr[] = $v; $cid = $v['id']; $arr = array_merge($arr,subtree($data,$cid,$lev+1)); } } return $arr; } //递归法找指定栏目的家谱树 function partree($data,$id=0){ $arr = array(); foreach($data as $v){ if($v['id'] == $id){ $arr[] = $v; $cid = $v['parent_id']; $arr = array_merge(partree($data,$cid),$arr); } } return $arr; } //迭代法查找指定栏目的家谱树 function fpartree($data,$id=0){ $arr = array(); while($id !== 0 ){ foreach($data as $v){ if($v['id'] == $id){ $arr[] = $v; $id = $v['parent_id']; break; } } } return array_reverse($arr); } //排序输出 $subtree = subtree($arr,0); foreach($subtree as $v){ echo str_repeat('  ',$v['lev']).$v['name'].'
'; } ?>