-3

Possible Duplicate:
How to create comma separated list from array in PHP?

I have an array as follows;

$array = array(1,2,3,4,5);

I want to print or echo this variable as 1,2,3,4,5. What is the simplest method for this?? I printed array[0] first and then skipped first value and used foreach function to echo all remaining ",".$value.

Community
  • 1
  • 1
Alfred
  • 21,058
  • 61
  • 167
  • 249

3 Answers3

2

Try following

echo implode(",", $array);
Cem Kalyoncu
  • 14,120
  • 4
  • 40
  • 62
1

You can use the implode function.

In the example you showed, it'd be written like this:

implode(',', $array);
Lumbendil
  • 2,906
  • 1
  • 19
  • 24
0
$array = array(1,2,3,4,5);
$result = implode(',', $array);
echo $result;
LazyOne
  • 158,824
  • 45
  • 388
  • 391