drake035
drake035

Reputation: 2897

Displaying a non associative array

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

Answers (4)

nl-x
nl-x

Reputation: 11832

and

echo implode("", $my_array);

"" being what is used as glue. (For example, try "<br/>\n")

Upvotes: 0

Marcin Orlowski
Marcin Orlowski

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

nl-x
nl-x

Reputation: 11832

foreach ($my_array as $val)
    echo $val;

Upvotes: 0

What have you tried
What have you tried

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

Related Questions