Reputation: 6788
I have searched the PHP.net site and originally thought of some use for the list()
function but doesn't seem to accomplish the goal:
I have an unknown number of values stored in a single array
$array1 = array(1,2,3,4,5);
or
$array1 = array(1,2,3);
I want to be able to echo (or print_r
) the values contained within an array to screen and separated only by commas and spacing.
For example:
the 'idea' is to have echo $array1
to display:
1,2,3
from the second example above.
Upvotes: 11
Views: 39274
Reputation: 1285
You can make use of list()
function if you know there are only 3 values.
$array = array('Life', 'living', 'Health');
list($life, $living, $health) = $array;
echo $life;
echo $living;
echo $health;
Note - you can make use of implode
function as well.
Upvotes: 0
Reputation: 54425
You can simply use PHP's implode function for this purpose as follows:
$string = implode(',', array(1,2,3,4,5));
Upvotes: 4
Reputation: 1454
http://us.php.net/manual/en/function.implode.php
echo implode(", ", $array);
Upvotes: 30