Reputation: 2897
Most articles about displaying an array in PHP seem to be for associative arrays. Is there a better way to display a non associative array than the following ?
for ( $i = 0; $i < sizeof( $my_array ); $i ++ ) {
echo $my_array[$i];
}
Upvotes: 0
Views: 208
Reputation: 11832
and
echo implode("", $my_array);
"" being what is used as glue. (For example, try "<br/>\n")
Upvotes: 0
Reputation: 75645
It's depends on what is the purpose of the display. If you just want to peek its content for debugging purposes then plain print_r()
or var_dump()
would suffice. Otherwise just loop as you do.
Upvotes: 2
Reputation: 11148
The way you're looping is just fine. However, I prefer to use foreach
in php
:
$array = array("1", "anotherItem", "more data");
foreach($array as $value){
echo $value;
}
Upvotes: 4