131

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

My array looks like this:

Array
(
    [0] => lorem
    [1] => ipsum
    [2] => dolor
    [3] => sit
    [4] => amet
)

How to transform this to a string like this with php?

$string = 'lorem, ipsum, dolor, sit, amet';
Community
  • 1
  • 1
m3tsys
  • 3,859
  • 6
  • 27
  • 43

4 Answers4

284
$arr = array ( 0 => "lorem", 1 => "ipsum", 2 => "dolor");

$str = implode (", ", $arr);
Josh
  • 7,934
  • 5
  • 41
  • 40
omabena
  • 3,411
  • 1
  • 15
  • 13
17

Directly from the docs:

$comma_separated = implode(",", $array);
Nobita
  • 23,059
  • 10
  • 56
  • 85
  • 2
    I used implode with a little modification. Since I needed a comma-separated list with single quotes as well, I used the following on my array: $innie = implode("', '", $arrayDistricts); [Note the single quotes inside the double quotes.] Then I used this in my query: IN ('$innie') [Note the single quotes around the variable.] – KiloVoltaire Aug 09 '15 at 03:27
  • Thanks Kilo thats exactly what i was looking for. I am using it in a bulk update – Sweet Chilly Philly Feb 08 '19 at 02:22
15

Make your array a variable and use implode.

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

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

Josh
  • 7,934
  • 5
  • 41
  • 40
Adam
  • 3,503
  • 6
  • 30
  • 50
10

You're looking for implode()

$string = implode(",", $array);

JKirchartz
  • 16,823
  • 7
  • 59
  • 87