Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to comma separate the values of an array for display

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.

like image 930
JM4 Avatar asked Jan 21 '11 19:01

JM4


2 Answers

http://us.php.net/manual/en/function.implode.php

echo implode(", ", $array);
like image 175
Hoatzin Avatar answered Oct 05 '22 18:10

Hoatzin


You can simply use PHP's implode function for this purpose as follows:

$string = implode(',', array(1,2,3,4,5));
like image 38
John Parker Avatar answered Oct 05 '22 19:10

John Parker