<li> <div>标记</div> </li>中的遍历数组和显示

时间:2011-09-19 14:30:38

标签: php

我想遍历这个数组并在项目符号中显示。我知道这看起来很可疑,就像你自己一样。但是我无法克服嵌套的foreach问题,特别是当谈到线程的deph时。我在上一个问题Traverse Array and Display In Bullet Points中得到了答案,但我不确定如何更改给出的解决方案中的标记....或者如果可能的话。下面是数组,我有一个指向我想要的标记的pastebin的链接,但是如果将来可以很容易地更改标记,那就太棒了,即标记视图与逻辑分离。

Array
(
    [1] => Array
        (
            [id] => 1
            [comment_title] => comment title
            [comment] => a
            [comment_date] => comment date
            [parent_id] => 0
            [children] => Array
                (
                    [3] => Array
                        (
                            [id] => 3
                            [comment_title] => comment title
                            [comment] => this is another comment
                            [comment_date] => comment date
                            [parent_id] => 1
                            [depth] => 0
                            [child_count] => 0
                            [children] => 
                        )

                    [4] => Array
                        (
                            [id] => 4
                            [comment_title] => comment title
                            [comment] => this is a third comment
                            [comment_date] => comment date
                            [parent_id] => 1
                            [depth] => 0
                            [child_count] => 0
                            [children] => 
                        )

                )

            [depth] => 1
            [child_count] => 2
        )

    [2] => Array
        (
            [id] => 2
            [comment_title] => comment title
            [comment] => this is yet another comment
            [comment_date] => comment date
            [parent_id] => 0
            [children] => Array
                (
                    [5] => Array
                        (
                            [id] => 5
                            [comment_title] => comment title
                            [comment] => another comment
                            [comment_date] => comment date
                            [parent_id] => 2
                            [children] => Array
                                (
                                    [7] => Array
                                        (
                                            [id] => 7
                                            [comment_title] => comment title
                                            [comment] => Hey im a comment!
                                            [comment_date] => 1/1/1970
                                            [parent_id] => 5
                                            [children] => Array
                                                (
                                                    [8] => Array
                                                        (
                                                            [id] => 8
                                                            [comment_title] => comment title
                                                            [comment] => I disagree
                                                            [comment_date] => 1/1/1970
                                                            [parent_id] => 7
                                                            [children] => Array
                                                                (
                                                                    [9] => Array
                                                                        (
                                                                            [id] => 8
                                                                            [comment_title] => comment title
                                                                            [comment] => My comment is...
                                                                            [comment_date] => 1/1/1970
                                                                            [parent_id] => 8
                                                                            [children] => Array
                                                                                (
                                                                                    [10] => Array
                                                                                        (
                                                                                            [id] => 8
                                                                                            [comment_title] => comment title
                                                                                          [comment] => Hey im a comment!
                                                                                   [comment_date] => 1/1/1970
                                                                                            [parent_id] => 9
                                                                                            [depth] => 0
                                                                                            [child_count] => 0
                                                                                            [children] => 
                                                                                        )

                                                                                )

                                                                            [depth] => 1
                                                                            [child_count] => 1
                                                                        )

                                                                )

                                                            [depth] => 2
                                                            [child_count] => 1
                                                        )

                                                )

                                            [depth] => 3
                                            [child_count] => 1
                                        )

                                )

                            [depth] => 4
                            [child_count] => 1
                        )

                    [6] => Array
                        (
                            [id] => 6
                            [comment_title] => comment title
                            [comment] => This is the last comment
                            [comment_date] => 1/1/1970
                            [parent_id] => 2
                            [depth] => 0
                            [child_count] => 0
                            [children] => 
                        )

                )

            [depth] => 5
            [child_count] => 2
        )

)

这是我想要使用的标记。 http://pastebin.com/MBppxiC7

2 个答案:

答案 0 :(得分:0)

在我之前提供给您的代码中,只需尝试添加代码片段,您就会了解其工作原理。一旦您开始理解我所制作的代码,您就可以扩展它并添加额外的功能。

答案 1 :(得分:0)

你可以尝试创建一个检查child_count的递归函数,如果它的&gt; 1那么函数将再次遍历该数组的子键以获取子项。基本上你可以创建一个函数,并在需要时调用它。