讨论板的多维数组

发布于 2024-10-12 20:35:01 字数 2342 浏览 3 评论 0原文

我有一个多维数组的讨论。

我正在使用递归函数(见下文)来回显该数组的值(注释)。但对于我的函数,仅出现第一个子注释(每个数组级别)。

我如何调整这个函数,以便我可以回显每个数组级别的所有子评论,就像在普通的讨论板中一样?

在此示例数组中,comment_id“4”和comment_id“7”位于同一级别,但使用我当前的函数,仅查看comment_id“4”评论。

Array
(
    [0] => Array
        (
            [comment_id] => 1
            [comment_content] => This is a comment...
            [child] => Array
                (
                    [0] => Array
                        (
                            [comment_id] => 3
                            [comment_content] => This is a reply to the comment...
                            [child] => Array
                                (
                                    [0] => Array
                                        (
                                            [comment_id] => 4
                                            [comment_content] => This is a reply to the reply...
                                            [child] => Array
                                                (
                                                )
                                        )

                                    [1] => Array
                                        (
                                            [comment_id] => 7
                                            [comment_content] => This is a another reply to the reply...
                                            [child] => Array
                                                (
                                                )
                                        )
                                )
                        )
                )
        )

    [1] => Array
        (
            [comment_id] => 2
            [comment_content] => This is another comment...
            [child] => Array
                (
                )

        )

    [2] => Array
        (
            [comment_id] => 6
            [comment_content] => This is another comment...
            [child] => Array
                (
                )
        )
)

我当前的功能如下所示:

function RecursiveWrite($array) {
    foreach ($array as $vals) {
        echo $vals['comment_content'] . "\n";
        RecursiveWrite($vals['child']);
    }
}

I have a multidimensional array of a discussion.

I'm using a recursive function (see below) to echo the values (comments) of this array. But with my function only the first child comment (per array level) appears.

How can I adapt this function, so that I can echo all child comments per array level, like in a normal discussion board?

In this example array the comment_id "4" and the comment_id "7" are on the same level, but with my current function only comment_id "4" comments is viewed.

Array
(
    [0] => Array
        (
            [comment_id] => 1
            [comment_content] => This is a comment...
            [child] => Array
                (
                    [0] => Array
                        (
                            [comment_id] => 3
                            [comment_content] => This is a reply to the comment...
                            [child] => Array
                                (
                                    [0] => Array
                                        (
                                            [comment_id] => 4
                                            [comment_content] => This is a reply to the reply...
                                            [child] => Array
                                                (
                                                )
                                        )

                                    [1] => Array
                                        (
                                            [comment_id] => 7
                                            [comment_content] => This is a another reply to the reply...
                                            [child] => Array
                                                (
                                                )
                                        )
                                )
                        )
                )
        )

    [1] => Array
        (
            [comment_id] => 2
            [comment_content] => This is another comment...
            [child] => Array
                (
                )

        )

    [2] => Array
        (
            [comment_id] => 6
            [comment_content] => This is another comment...
            [child] => Array
                (
                )
        )
)

My current function looks like this:

function RecursiveWrite($array) {
    foreach ($array as $vals) {
        echo $vals['comment_content'] . "\n";
        RecursiveWrite($vals['child']);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪装你 2024-10-19 20:35:01

也许我不明白你的问题,但功能看起来不错。

我刚刚在你的函数中添加了 spacer 和 comment_id

function RecursiveWrite($array, $spacer='') {
    foreach ($array as $vals) {
        echo $spacer . $vals['comment_id'] . ' - ' . $vals['comment_content'] . "\n";
        RecursiveWrite($vals['child'], $spacer . '  ');
    }
}

它输出

1 - This is a comment...
  3 - This is a reply to the comment...
    4 - This is a reply to the reply...
    7 - This is a another reply to the reply...
2 - This is another comment...
6 - This is another comment...

Maybe i don't understand you question, but function seems fine.

I've just added spacer and comment_id into your function

function RecursiveWrite($array, $spacer='') {
    foreach ($array as $vals) {
        echo $spacer . $vals['comment_id'] . ' - ' . $vals['comment_content'] . "\n";
        RecursiveWrite($vals['child'], $spacer . '  ');
    }
}

And it outputs

1 - This is a comment...
  3 - This is a reply to the comment...
    4 - This is a reply to the reply...
    7 - This is a another reply to the reply...
2 - This is another comment...
6 - This is another comment...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文