<?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 subtree($arr,$id=0) {
static $subs = array();
foreach($arr as $k=>$v) {
if($v['parent'] == $id) {
$subs[] = $v;
subtree($arr,$v['id']);
}
}
return $subs;
}
print_r(subtree($area,0,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 subtree($arr,$id=0) {
static $subs = array();
foreach($arr as $k=>$v) {
if($v['parent'] == $id) {
$subs[] = $v;
subtree($arr,$v['id']);
}
}
return $subs;
}
print_r(subtree($area,0,1));
?>