Echo a multidimensional array in PHP

2019-01-06 20:59发布

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

9条回答
甜甜的少女心
2楼-- · 2019-01-06 21:25

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

查看更多
该账号已被封号
3楼-- · 2019-01-06 21:25
<pre>
<?php print_r ($array); ?>
</pre>
查看更多
仙女界的扛把子
4楼-- · 2019-01-06 21:32

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

查看更多
对你真心纯属浪费
5楼-- · 2019-01-06 21:33

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:

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

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

查看更多
Lonely孤独者°
6楼-- · 2019-01-06 21:38

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!

查看更多
放我归山
7楼-- · 2019-01-06 21:39

print_r($arr) usually gives pretty readable result.

查看更多
登录 后发表回答