PHP中找家谱树的几种方法
<?php $area = array( array('id'=>1,'name'=>'安徽','parent'=>0), array('id'=>2,'name'=>'海淀','parent'=>7), array('id'=>3,'name'=>'濉溪县','parent'=>5), array('id'=>4,'name'=>'昌平','parent'=>7), array('id'=>5,'name'=>'淮北','parent'=>1), array('id'=>6,'name'=>'朝阳','parent'=>7), array('id'=>7,'name'=>'北京','parent'=>0), array('id'=>8,'name'=>'上地','parent'=>2) ); function DieDai($arr,$id){ $subtree=array(); while($id){ foreach($arr as $val){ if($val['id']==$id){ $subtree[]=$val; if($val['parent']!==0){ $id=$val['parent']; }else{ break 2; } } } } return $subtree; } echo "<pre>"; print_r(DieDai($area,3)); function DiGui($arr,$id){ static $root=array(); foreach($arr as $val){ if($val['id']==$id){ $root[]=$val; if($val['parent']!==0){ $id1=$val['parent']; DiGui($arr,$id1); }else{ break; } } } return $root; } print_r(DiGui($area,3)); function findfam($arr,$area){ static $fam=array(); foreach($arr as $val){ if($val['name']==$area){ $parentid=$val['parent']; foreach($arr as $v){ if($v['id']==$parentid){ $area=$v['name']; $fam[]=$area; findfam($arr,$area); } } } } return $fam; } print_r(findfam($area,'濉溪县')); echo "</pre>";