16

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
                (
                )
        )
)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Jennifer
  • 217
  • 1
  • 2
  • 5
  • It would depend which order you'd like it to be echo'ed: breadth-first or depth-first? --Just kidding, you already included the order you wanted – jerluc Jan 18 '11 at 00:20
  • Are you wanting to echo this for the purposes of display to a user or for the purposes of development checking / debugging the contents of the array? – MrEyes Jan 18 '11 at 00:22

9 Answers9

30
<pre>
<?php print_r ($array); ?>
</pre>
Hilydrow
  • 918
  • 1
  • 6
  • 15
17

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

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
keithjgrant
  • 12,421
  • 6
  • 54
  • 88
6

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/

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
Dung
  • 19,199
  • 9
  • 59
  • 54
  • Proper, better, and cleaner to check that each value is an array and generic enough to handle any nested arrays. – ptay Jun 24 '18 at 01:04
2

print_r($arr) usually gives pretty readable result.

andbi
  • 4,426
  • 5
  • 45
  • 70
1

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']);
    }
}
Muhammad Tahir
  • 2,351
  • 29
  • 25
1

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!

Matt Lowden
  • 2,586
  • 17
  • 19
  • Good idea on the string capture. Be wary of concatenation in a loop though. If the array is of any significant size (or depth), you'll probably be better off with echoes and wrapping the function with `ob_start()` and `ob_get_clean()` – keithjgrant Jan 18 '11 at 00:38
0

Try to use var_dump function.

ikostia
  • 7,395
  • 6
  • 30
  • 39
0

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

Matt V.
  • 9,703
  • 10
  • 35
  • 56
0

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/

jhogendorn
  • 5,931
  • 3
  • 26
  • 35