Echo a multidimensional array in PHP

后端 未结 9 1527
名媛妹妹
名媛妹妹 2020-11-27 07:24

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 nest

相关标签:
9条回答
  • 2020-11-27 07:59

    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!

    0 讨论(0)
  • 2020-11-27 08:10

    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/

    0 讨论(0)
  • 2020-11-27 08:10

    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']);
        }
    }
    
    0 讨论(0)
  • 2020-11-27 08:11

    print_r($arr) usually gives pretty readable result.

    0 讨论(0)
  • 2020-11-27 08:15

    Try to use var_dump function.

    0 讨论(0)
  • 2020-11-27 08:21

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

    0 讨论(0)
提交回复
热议问题