0

我有一个这样构建的数组

大批
(
    [6] => 数组
        (
            [id] => 6
            [parent_id] =>
            [文本] => 顶级评论
            [等级] => 1
        )

    [1] => 数组
        (
            [id] => 1
            [parent_id] =>
            [文本] => 顶级评论
            [等级] => 1
            [孩子] => 数组
                (
                    [2] => 数组
                        (
                            [id] => 2
                            [parent_id] => 1
                            [文本] => 对#1 的回应
                        )

                    [3] => 数组
                        (
                            [id] => 3
                            [parent_id] => 1
                            [文本] => 对#1 的回应
                            [孩子] => 数组
                                (
                                    [4] => 数组
                                        (
                                            [id] => 4
                                            [parent_id] => 3
                                            [文本] => 对#3 的回应
                                        )

                                )

                        )

                    [10] => 数组
                        (
                            [id] => 10
                            [parent_id] => 1
                            [文本] => 对 #2 的回应
                            [孩子] => 数组
                                (
                                    [11] => 数组
                                        (
                                            [id] => 11
                                            [parent_id] => 10
                                            [文本] => 对#10 的回应
                                            [孩子] => 数组
                                                (
                                                    [13] => 数组
                                                        (
                                                            [id] => 13
                                                            [parent_id] => 11
                                                            [文本] => 回复 #11
                                                            [孩子] => 数组
                                                                (
                                                                    [14] => 数组
                                                                        (
                                                                            [id] => 14
                                                                            [parent_id] => 13
                                                                            [文本] => 回复 #13
                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                    [12] => 数组
                                        (
                                            [id] => 12
                                            [parent_id] => 10
                                            [文本] => 对#10 的回应
                                        )

                                )

                        )

                )

        )

    [5] => 数组
        (
            [id] => 5
            [parent_id] =>
            [文本] => 顶级评论
            [等级] => 1
        )

    [9] => 数组
        (
            [id] => 9
            [parent_id] =>
            [文本] => 顶级评论
            [等级] => 1
        )

)

JSON

{"6":{"id":6,"parent_id":null,"text":"顶级评论","level":1},"1":{"id":1,"parent_id": null,"text":"顶级评论","level":1,"children":{"2":{"id":2,"parent_id":1,"text":"对#1的回应" },"3":{"id":3,"parent_id":1,"text":"响应 #1","children":{"4":{"id":4,"parent_id": 3,"text":"对#3的响应"}}},"10":{"id":10,"parent_id":1,"text":"对#2的响应","children":{" 11":{"id":11,"parent_id":10,"text":"响应 #10","children":{"13":{"id":13,"parent_id":11,"text":"响应 #11","children":{"14":{"id":14,"parent_id":13," text":"对#13的响应"}}}}},"12":{"id":12,"parent_id":10,"text":"对#10的响应"}}}}},"5 ":{"id":5,"parent_id":null,"text":"顶级评论","level":1},"9":{"id":9,"parent_id":null," text":"顶级评论","level":1}}parent_id":10,"text":"响应 #10"}}}}},"5":{"id":5,"parent_id":null,"text":"顶级评论","level ":1},"9":{"id":9,"parent_id":null,"text":"顶级评论","level":1}}parent_id":10,"text":"响应 #10"}}}}},"5":{"id":5,"parent_id":null,"text":"顶级评论","level ":1},"9":{"id":9,"parent_id":null,"text":"顶级评论","level":1}}

什么是最简单的呼应和风格的方法?像reddit一样说。我想在视图文件中进行样式设置。

4

1 回答 1

0

您可以尝试使用http://ellislab.com/codeigniter/user-guide/helpers/html_helper.html#ol_and_ul,但我宁愿使用循环数组并生成 HTML 的自定义函数。

编辑:

function draw_comments($comments, $level = 0)
{
    echo  '<ul class="level' . $level . '">';
    foreach($comments as $comment)
    {
        draw_comment($comment);

        if(!empty($comment['children']))
            draw_comments($comment['children'], $level+1);
    }
    echo '</ul>';
}

function draw_comment($comment)
{
    echo '<li>';
    echo $comment['text'];
    echo '</li>';
}

draw_comments($comments_array);
于 2013-03-02T11:51:23.383 回答