在 PHP 中回显多维数组

发布于 2024-10-12 23:07:03 字数 1479 浏览 7 评论 0原文

我有一个多维数组,我试图找出如何简单地“回显”数组的元素。数组的深度未知,因此可以嵌套得很深。

对于下面的数组,正确的 echo 顺序是:

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

这是我正在讨论的数组:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

寄居人 2024-10-19 23:07:03
<pre>
<?php print_r ($array); ?>
</pre>
<pre>
<?php print_r ($array); ?>
</pre>
一袭白衣梦中忆 2024-10-19 23:07:03

看起来您只是想从每个数组中写入一个重要值。尝试像这样的递归函数:

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

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

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

痴情换悲伤 2024-10-19 23:07:03

正确、更好、干净的解决方案:

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";
        }
    }
}

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

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

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

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/

恏ㄋ傷疤忘ㄋ疼 2024-10-19 23:07:03

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

print_r($arr) usually gives pretty readable result.

好菇凉咱不稀罕他 2024-10-19 23:07:03

如果你想将它存储为变量,你可以这样做:

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

显然你可以根据需要格式化!

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!

雪若未夕 2024-10-19 23:07:03

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

1) - print_r($array); 或者如果您想要格式良好的数组,则

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

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

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

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

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

There are multiple ways to do that

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

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.

function recursiveFunction($array) {
    foreach ($array as $val) {
            echo $val['comment_content'] . "\n";
            recursiveFunction($vals['child']);
    }
}
忆梦 2024-10-19 23:07:03

尝试使用 var_dump 函数。

Try to use var_dump function.

烟雨凡馨 2024-10-19 23:07:03

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

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

梦晓ヶ微光ヅ倾城 2024-10-19 23:07:03

递归通常是您的答案,但另一种选择是使用引用。请参阅 http://www.ideashower .com/our_solutions/create-a-parent-child-array-struct-in-one-pass/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文