【问题标题】:Echo a multidimensional array in PHP在 PHP 中回显一个多维数组
【发布时间】:2011-06-10 19:09:30
【问题描述】:

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

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

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

【问题讨论】:

  • 这将取决于您希望它被回显的顺序:广度优先还是深度优先? --开个玩笑,你已经包含了你想要的顺序
  • 您是想回显这个以显示给用户还是为了开发检查/调试数组的内容?

标签: php arrays multidimensional-array nested


【解决方案1】:

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

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-multidimensional-array/

【讨论】:

  • 正确、更好、更清晰地检查每个值是否是一个数组并且足够通用以处理任何嵌套数组。
【解决方案2】:

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

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']);
    }
}

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

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

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

        【讨论】:

        • 关于字符串捕获的好主意。不过要小心循环中的串联。如果数组具有任何显着的大小(或深度),您可能会更好地使用 echoes 并使用 ob_start()ob_get_clean() 包装函数
        【解决方案6】:

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

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

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

        【讨论】:

        • 不是foreach($array as $elem)吗? :) 不错的答案!
        • @eric-gagnon: 是的,你刚刚打败了我。固定。
        【解决方案7】:

        尝试使用var_dump函数。

        【讨论】:

          【解决方案8】:
          <pre>
          <?php print_r ($array); ?>
          </pre>
          

          【讨论】:

            【解决方案9】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-01-10
              • 2017-07-19
              • 1970-01-01
              • 2015-09-15
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多