【问题标题】:multidimensional array for a discussion board讨论板的多维数组
【发布时间】: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


【解决方案1】:

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

我刚刚在你的函数中添加了 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...

【讨论】:

  • 你完全正确......功能还可以......我做错了其他事情。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多