<?php $are=array( ['id'=>1,'name'=>'安徽','parent'=>0
<?php
$are=array(
['id'=>1,'name'=>'安徽','parent'=>0],
['id'=>2,'name'=>'海淀','parent'=>7],
['id'=>3,'name'=>'淮溪县','parent'=>5],
['id'=>4,'name'=>'昌平县','parent'=>7],
['id'=>5,'name'=>'淮北','parent'=>1],
['id'=>6,'name'=>'朝阳','parent'=>7],
['id'=>7,'name'=>'北京','parent'=>0],
['id'=>8,'name'=>'上地','parent'=>2]
);
/*思路
先找到自己
把parent赋值给$id
然后while判断id是不是0 做循环
*/
function framily($data,$id){
static $far=array();
while ($id!=0) {
foreach ($data as $v) {
if ($v['id']==$id) {//先找到自己
$far[]=$v;//递和归的顺序换下 显示的顺序就反了
$id=$v['parent'];
break;
}
}
}
return $far;
}
print_r(framily($are,8));