Christy Christy - 8 months ago 57
PHP Question

How do I print a multidimensional array that varies with depth in php?

I have the following example of array output. I need to find a way to convert this array to a string or output the emails in an organized way. I have tried implode but I only receive an "Array" output. Please help!

Array
(
[0] => Array
(
[0] => Array
(
[0] => Email
)

[1] => Array
(
[0] => test1@gmail.com
)

[2] => Array
(
[0] => test2@gmail.com
)

[3] => Array
(
[0] => test3@gmail.com
)

[4] => Array
(
[0] => test4@gmail.com
)

[5] => Array
(
[0] => test5@gmail.com
)

)

)

Answer Source
for ($i = 0; $i < count($arr[0]); $i++) {
    for ($j = 0; $j < count($arr[0][$i]); $j++) {
        echo $arr[0][$i][$j];
    }
}

The above will output each string of the array.
The only question I have is why you are storing single strings so deep in an array? Perhaps we could see more of the code?

The below code would be simpler for what you provided:

$arr[] = "Email";
$arr[] = "test1@gmail.com";
$arr[] = "test2@gmail.com";
$arr[] = "test3@gmail.com";
$arr[] = "test4@gmail.com";
$arr[] = "test5@gmail.com";

for ($i = 0; $i < count($arr); $i++) {
    echo $arr[$i];
}

Even better, use array keys instead:

$arr["Email"][] = "test1@gmail.com";
$arr["Email"][] = "test2@gmail.com";
$arr["Email"][] = "test3@gmail.com";
$arr["Email"][] = "test4@gmail.com";
$arr["Email"][] = "test5@gmail.com";

for ($i = 0; $i < count($arr["Email"]); $i++) {
    echo $arr["Email"][$i];
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download