具体代码
<?php
$arr = array( 0=>array( 'cid'=>1, 'pid'=>0, 'name'=>'亚洲',
), 1=>array( 'cid'=>2, 'pid'=>0, 'name'=>'北美洲',
), 2=>array( 'cid'=>3, 'pid'=>1, 'name'=>'中国',
), 3=>array( 'cid'=>4, 'pid'=>2, 'name'=>'美国',
), 4=>array( 'cid'=>5, 'pid'=>3, 'name'=>'北京',
), 5=>array( 'cid'=>6, 'pid'=>3, 'name'=>'河北',
), 6=>array( 'cid'=>7, 'pid'=>5, 'name'=>'东城区',
), 7=>array( 'cid'=>8, 'pid'=>5, 'name'=>'海淀区',
), 8=>array( 'cid'=>9, 'pid'=>7, 'name'=>'东城区金融街',
), 9=>array( 'cid'=>10, 'pid'=>9, 'name'=>'金融街长安大厦A座',
),
);
function GetTree($arr,$pid,$step){
global $tree;
foreach($arr as $key=>$val) {
if($val['pid'] == $pid) {
$flg = str_repeat('└―',$step);
$val['name'] = $flg.$val['name'];
$tree[] = $val;
GetTree($arr , $val['cid'] ,$step+1);
}
} return $tree;
}
$new_arr=GetTree($arr,$pid=0,$step=0);
?>
<select id="haha" name="hehe">
<?php
foreach($new_arr as $k => $v){
?>
<option value=""><?php echo $v['name']?></option>
<?php
}
?>
</select>
实现效果