-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
  • 20,303
  • 60
  • 160
  • 239

3 Answers3

2

Try following

echo implode(",", $array);
Cem Kalyoncu
  • 13,474
  • 4
  • 40
  • 60
1

You can use the implode function.

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

implode(',', $array);
Lumbendil
  • 2,896
  • 1
  • 18
  • 24
0
$array = array(1,2,3,4,5);
$result = implode(',', $array);
echo $result;
LazyOne
  • 148,457
  • 42
  • 363
  • 369