$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 subtree($arr,$id,$lev) {
static $subs = array();
foreach($arr as $v) {
if($v['parent'] == $id) {
$v['lev'] = $lev;
$subs[] = $v;
subtree($arr,$v['id'],$lev+1);
}
}
return $subs;
}
$tree = subtree($area,0,1);
foreach($tree as $v) {
echo str_repeat(' ',$v['lev']).$v['name'].'<br />';
}
无限极分类查找子孙树
最新推荐文章于 2019-12-25 15:38:26 发布