【发布时间】:2011-06-11 11:44:11
【问题描述】:
我有一个讨论的多维数组。
我正在使用递归函数(见下文)来回显该数组的值(cmets)。但使用我的函数时,只会出现第一个子注释(每个数组级别)。
如何调整此功能,以便可以回显每个数组级别的所有子 cmets,就像在普通讨论板中一样?
在这个示例数组中,comment_id "4" 和 comment_id "7" 处于同一级别,但我当前的函数只查看了 comment_id "4" cmets。
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']);
}
}
【问题讨论】:
-
你能发布一个你正在使用的数组的例子吗?
-
嘿詹妮弗,你在这里发布的代码很好。您必须为我们发布更多代码以帮助您找到错误。如果根本没有打印注释 7,那么可能 (a) 您的循环中有一个“break”或“return”语句,或者 (b) 有一个错误导致整个脚本结束。或者如果代码相当复杂,它可以是任何东西,真的。
标签: php recursion multidimensional-array