讨论板的多维数组
我有一个多维数组的讨论。
我正在使用递归函数(见下文)来回显该数组的值(注释)。但对于我的函数,仅出现第一个子注释(每个数组级别)。
我如何调整这个函数,以便我可以回显每个数组级别的所有子评论,就像在普通的讨论板中一样?
在此示例数组中,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许我不明白你的问题,但功能看起来不错。
我刚刚在你的函数中添加了 spacer 和 comment_id
它输出
Maybe i don't understand you question, but function seems fine.
I've just added spacer and comment_id into your function
And it outputs