FLEA中二叉树数组的遍历输出

太久没更新了,更新一篇关于后台的文章吧。

最近在做一个项目其中涉及到“无限级回复”,FLEA中中有一个关于数组的辅助类:FLEA_Helper_Array,这个类里面有一个非常强大的数组处理方法:array_to_tree,这个方法可以把二维数组转换为二叉树结构。但是要怎样遍历这个方法产生的二叉树数组呢?以下是我的做法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
function preTree($cat){	
    foreach ($cat as $c){
        ?>
        <p><a href="http://<?=$c['poper_site']?>"><?=$c['poper']?></a>:<?=t($c['content'])?></p>
 
        <?php if(isset($c['childrens'])){?>
        <ul>
        <?php foreach ($c['childrens'] as $s){?>
            <li><p><a href="http://<?=$s['poper_site']?>"><?=$s['poper']?></a>:<?=t($s['content'])?></p>
                <?php 
                if(isset($s['childrens'])){
                    ?><ul><li><?php
                    $this->preTree($s['childrens']);
                    ?></li></ul><?php
                }
                ?>
 
            </li>
            <?php }?>
        </ul>
        <?php }?>
    <?php 
    }
}
?>

用的是递归的方法。

No Response to FLEA中二叉树数组的遍历输出 »

还没有任何评论。

Leave a Reply

Email address is not published

You should say a Chinese word to pass spam check. If you can not input Chinese, just copy 你好 and paste them into comment text box.