标签:blog io ar for sp div on cti log
1:迭代的效率要比递归高,代码页相对少些,所以查找家谱树推荐用迭代
$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 gettree($arr,$id){ $tree = array(); while($id !=0){ foreach($arr as $v){ if($v[‘id‘] == $id){ $tree[] = $v; //把找到的子数组放进空的tree数组里 $id = $v[‘parent‘]; //while循环时用到的。 break; } } } return $tree; } print_r(gettree($area,8));
标签:blog io ar for sp div on cti log
原文地址:http://www.cnblogs.com/a2762/p/4028502.html