Php 讨论板的多维数组

Php 讨论板的多维数组,php,recursion,multidimensional-array,Php,Recursion,Multidimensional Array,我有一个多维数组的讨论 我使用一个递归函数(见下文)来回显这个数组的值(注释)。但是对于我的函数,只会显示第一个子注释(每个数组级别) 如何调整此函数,以便能够像在普通讨论板中一样,按数组级别回显所有子注释 在本示例数组中,注释id“4”和注释id“7”处于同一级别,但使用我的当前函数,仅查看注释id“4”注释 Array ( [0] => Array ( [comment_id] => 1 [comment_c

我有一个多维数组的讨论

我使用一个递归函数(见下文)来回显这个数组的值(注释)。但是对于我的函数,只会显示第一个子注释(每个数组级别)

如何调整此函数,以便能够像在普通讨论板中一样,按数组级别回显所有子注释

在本示例数组中,注释id“4”和注释id“7”处于同一级别,但使用我的当前函数,仅查看注释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']);
    }
}

也许我不明白你的问题,但功能似乎很好

我刚刚在您的函数中添加了间隔符和注释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...

也许我不明白你的问题,但功能似乎很好

我刚刚在您的函数中添加了间隔符和注释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...

你能发布一个你正在使用的数组的例子吗?嘿,詹妮弗,你在这里发布的代码很好。您必须为我们发布更多代码,以帮助您找到bug。如果注释7根本没有打印出来,那么可能(a)循环中有一个“break”或“return”语句,或者(b)有一个错误结束了整个脚本。或者,如果代码相当复杂,它可能是任何东西,真的。你能发布一个你正在使用的数组的示例吗?嘿,詹妮弗,你在这里发布的代码很好。您必须为我们发布更多代码,以帮助您找到bug。如果注释7根本没有打印出来,那么可能(a)循环中有一个“break”或“return”语句,或者(b)有一个错误结束了整个脚本。或者如果代码相当复杂,它可以是任何东西,真的。你完全正确…函数还可以。。。我做错了别的事。非常感谢。你完全正确…功能还可以。。。我做错了别的事。谢谢。