142

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';
0

4 Answers 4

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

$str = implode (", ", $arr);
Sign up to request clarification or add additional context in comments.

2 Comments

What if is it array inside array.
Create a loop over the parent array or use array_map. @ankitsuthar
21

Directly from the docs:

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

2 Comments

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.]
Thanks Kilo thats exactly what i was looking for. I am using it in a bulk update
19

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

Comments

11

You're looking for implode()

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.