求一个显示树形数组的方法。

achuanok 2009-08-11 04:06:29
$array = array(array('id' => '10', 'name' => 'aaa', 'nodes' =>array()),
array('id' => '11', 'name' => 'bbb','nodes' =>array('id' => '21', 'name' => 'aaaaaa','nodes'=> array(‘这里面有无限分级’))),
array('id' => '12', 'name' => 'ccc', 'nodes' =>array()),
array('id' => '13', 'name' => 'ddd', 'nodes' =>array('id' => '23', 'name' => 'cccccc','nodes'=> array())),
array('id' => '14', 'name' => 'eee', 'nodes' =>array())
);
希望显示结果
aaa
bbb
aaaaaaa
下面无限分极
ccc
ddd
ccccccc
eee

想求一个直接输出这个效果的函数,先谢了
...全文
68 点赞 收藏 7
写回复
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lincog007 2009-08-14
学习,顶递归
回复
YHL27 2009-08-14
学习!!
回复
绿绦 2009-08-13
up
回复
llj480028 2009-08-11
学习!
回复
CYBEREXP2008 2009-08-11
楼上的正解
回复
CunningBoy 2009-08-11
用递归函数:

function dispalyArray($array){
var tabindex = " ".tabindex;
foreach( $array as $k => $v){
if( $k == "name" ){
echo tabindex.$v."\r\n";
}
if( $k == "node" && count($array[$k]) > 0 ){
displayArray($array[$k]);
}
}
}
回复
fenyao 2009-08-11



$array = array(array('id' => '10', 'name' => 'aaa', 'nodes' =>array()),
array('id' => '11', 'name' => 'bbb','nodes' =>array('id' => '21', 'name' => 'aaaaaa','nodes'=> array("11"=>"haha"))),
array('id' => '12', 'name' => 'ccc', 'nodes' =>array()),
array('id' => '13', 'name' => 'ddd', 'nodes' =>array('id' => '23', 'name' => 'cccccc','nodes'=> array())),
array('id' => '14', 'name' => 'eee', 'nodes' =>array())
);
function FormatTagString($num){
$tag = "-";
$string="";
for ($i=0;$i<$num;$i++){
$string.=$tag;
}
return $string;
}

function GetTree($array,$level){
global $tree;
foreach ($array as $k=>$v){
if (!is_array($v))$tree.=FormatTagString($level).$v."<br />";
else GetTree($v,$level+1);
}
return $tree;
}

$string = GetTree($array,1);
print_r($string);




试试看
回复
发动态
发帖子
基础编程
创建于2007-09-28

2.1w+

社区成员

从PHP安装配置,PHP入门,PHP基础到PHP应用
申请成为版主
社区公告
暂无公告