Traverse Array and Display in <li><div> markup


Traverse Array and Display in <li><div> markup

中遍历数组和显示标记。

我想遍历这个数组并显示,在项目符号div中。我知道这看起来很可疑,你自己动手吧。但是我无法克服每个问题的嵌套,特别是当涉及到线程的深度时。我得到了一个答案在上一个问题遍历数组和显示在子弹点,但我不知道如何改变在解决方案中给出....的标记或者如果可能的话。下面是数组,我有一个链接到我想标记的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

在我之前提供的代码中,只需尝试添加代码片段,您就会了解它是如何工作的。一旦您开始理解我编写的代码,您将能够扩展它并添加额外的功能。

您可以尝试创建一个递归函数,该函数检查child_count,如果其>1,则函数将再次遍历该数组的子键以获取子键。基本上,您可以创建一个函数,并在需要时调用它。