[size=x-small]废话不多说:直接代码,把下面的代码访问以下在本地
$arr = array(
array('id' => '3', 'name' => "上海", 'parid' => 4,'lev'=>4),
array('id' => '4', 'name' => "上海1", 'parid' => 5,'lev'=>3),
array('id' => '5', 'name' => "上海2", 'parid' => 6,'lev'=>2),
array('id' => '6', 'name' => "上海3", 'parid' => 0,'lev'=>1),
array('id' => '13', 'name' => "北京", 'parid' => 14,'lev'=>4),
array('id' => '14', 'name' => "北京1", 'parid' => 15,'lev'=>3),
array('id' => '15', 'name' => "北京2", 'parid' => 16,'lev'=>2),
array('id' => '16', 'name' => "北京3", 'parid' => 0,'lev'=>1),
);
function tree($list,$pid=0,$level=0,$html='--'){
static $tree = array();
foreach($list as $v){
if($v['parid'] == $pid){
$v['lev'] = $level;
$v['html'] = str_repeat($html,$level);
$tree[] = $v;
tree($list,$v['id'],$level+1);
}
}
return $tree;
}
$data=tree($arr,$pid=0,$level=0,$html='--');
var_dump($data);
foreach($data as $v){
echo "
";
echo $v['html'].$v['name'];
}
?>
访问结果如下:
[img]http://dl2.iteye.com/upload/attachment/0104/6262/d411ce9d-5bfc-34a6-b5ea-1294bd066b46.png[/img]
有些知识多说也说不明白,程序结果更令人信服,希望对你们有帮助[/size]