Echo在PHP中的多维数组

Echo在PHP中的多维数组

本文介绍了Echo在PHP中的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多维数组,我试图找出如何简单回声的数组的元素。该阵列的深度是未知的,所以它可以被深深嵌套

在下面的阵列的情况下,以正确的顺序来呼应将是:

 这是一个父评论
这是一个孩子评论
这是第二个孩子意见
这是另一个父评论

这是我说的数组:

 阵列

    [0] =>排列
        (
            [COMMENT_ID] => 1
            [COMMENT_CONTENT] =>这是一个父评论
            [儿童] =>排列
                (
                    [0] =>排列
                        (
                            [COMMENT_ID] => 3
                            [COMMENT_CONTENT] =>这是一个孩子评论
                            [儿童] =>排列
                                (
                                    [0] =>排列
                                        (
                                            [COMMENT_ID] => 4
                                            [COMMENT_CONTENT] =>这是第二个孩子意见
                                            [儿童] =>排列
                                                (
                                                )
                                        )
                                )
                        )
                )
        )    [1] =>排列
        (
            [COMMENT_ID] => 2
            [COMMENT_CONTENT] =>这是另一个父评论
            [儿童] =>排列
                (
                )
        )


解决方案

它看起来像你只是想写从每个阵列中的一个重要的应用价值。尝试递归函数像这样:

 函数RecursiveWrite($数组){
    的foreach($数组作为$瓦尔斯){
        回声$瓦尔斯['COMMENT_CONTENT']。 \\ n;
        RecursiveWrite($瓦尔斯['孩子']);
    }
}

您也可以让它多了几分动感,并有'COMMENT_CONTENT '孩子'字符串传递到函数作为参数(并继续传递它们的递归调用)。

I have a multidimensional array and I'm trying to find out how to simply "echo" the elements of the array. The depth of the array is not known, so it could be deeply nested.

In the case of the array below, the right order to echo would be:

This is a parent comment
This is a child comment
This is the 2nd child comment
This is another parent comment

This is the array I was talking about:

Array
(
    [0] => Array
        (
            [comment_id] => 1
            [comment_content] => This is a parent comment
            [child] => Array
                (
                    [0] => Array
                        (
                            [comment_id] => 3
                            [comment_content] => This is a child comment
                            [child] => Array
                                (
                                    [0] => Array
                                        (
                                            [comment_id] => 4
                                            [comment_content] => This is the 2nd child comment
                                            [child] => Array
                                                (
                                                )
                                        )
                                )
                        )
                )
        )

    [1] => Array
        (
            [comment_id] => 2
            [comment_content] => This is another parent comment
            [child] => Array
                (
                )
        )
)
解决方案

It looks like you're only trying to write one important value from each array. Try a recursive function like so:

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

You could also make it a little more dynamic and have the 'comment_content' and 'child' strings passed into the function as parameters (and continue passing them in the recursive call).

这篇关于Echo在PHP中的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:34