在 PHP 中回显多维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4719326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:08:07  来源:igfitidea点击:

Echo a multidimensional array in PHP

phparraysmultidimensional-arraynested

提问by Jennifer

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
                (
                )
        )
)

采纳答案by keithjgrant

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).

您还可以让它更动态一些,'comment_content'并将 和'child'字符串作为参数传递到函数中(并继续在递归调用中传递它们)。

回答by Hilydrow

<pre>
<?php print_r ($array); ?>
</pre>

回答by Dung

Proper, Better, and Clean Solution:

正确、更好和清洁的解决方案:

function traverseArray($array)
{
    // Loops through each element. If element again is array, function is recalled. If not, result is echoed.
    foreach ($array as $key => $value)
    {
        if (is_array($value))
        {
            Self::traverseArray($value); // Or
            // traverseArray($value);
        }
        else
        {
            echo $key . " = " . $value . "<br />\n";
        }
    }
}

You simply call in this helper function traverseArray($array)in your current/main class like this:

您只需在traverseArray($array)当前/主类中调用此辅助函数,如下所示:

$this->traverseArray($dataArray); // Or
// traverseArray($dataArray);

source: http://snipplr.com/view/10200/recursively-traverse-a-multidimensional-array/

来源:http: //snipplr.com/view/10200/recursively-traverse-a-multidimensional-array/

回答by andbi

print_r($arr)usually gives pretty readable result.

print_r($arr)通常会给出非常可读的结果。

回答by Matt Lowden

if you wanted to store it as a variable you could do:

如果您想将其存储为变量,您可以执行以下操作:

recurse_array($values){
    $content = '';
    if( is_array($values) ){
        foreach($values as $key => $value){
            if( is_array($value) ){
                $content.="$key<br />".recurse_array($value);
            }else{
                $content.="$key = $value<br />";
            }

        }
    }
    return $content;
}

$array_text = recurse_array($array);

Obviously you can format as needed!

显然,您可以根据需要进行格式化!

回答by Matt V.

If you're outputting the data for debugging and development purposes, Krumois great for producing easily readable output. Check out the example output.

如果您出于调试和开发目的输出数据,Krumo非常适合生成易于阅读的输出。查看示例输出

回答by jhogendorn

Recursion would be your answer typically, but an alternative would be to use references. See http://www.ideashower.com/our_solutions/create-a-parent-child-array-structure-in-one-pass/

递归通常是您的答案,但另一种方法是使用引用。见http://www.ideashower.com/our_solutions/create-a-parent-child-array-structure-in-one-pass/

回答by Muhammad Tahir

There are multiple ways to do that

有多种方法可以做到这一点

1) - print_r($array);or if you want nicely formatted array then

1) -print_r($array);或者如果你想要格式很好的数组,那么

echo '<pre>'; print_r($array); echo '<pre/>';

//-------------------------------------------------

2) - use var_dump($array)to get more information of the content in the array like datatype and length. //-------------------------------------------------

3) - you can loop the array using php's foreach();and get the desired output.

//------------------------------------------------ -

2) - 用于var_dump($array)获取数组中内容的更多信息,如数据类型和长度。//------------------------------------------------ -

3) - 您可以使用 php 循环数组foreach();并获得所需的输出。

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

回答by ikostia

Try to use var_dumpfunction.

尝试使用var_dump函数。