How to transform array to comma separated words string?

PhpArraysString

Php Problem Overview


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

Php Solutions


Solution 1 - Php

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

$str = implode (", ", $arr);

Solution 2 - Php

Directly from the docs:

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

Solution 3 - Php

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

Solution 4 - Php

You're looking for implode()

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionm3tsysView Question on Stackoverflow
Solution 1 - PhpomabenaView Answer on Stackoverflow
Solution 2 - PhpNobitaView Answer on Stackoverflow
Solution 3 - PhpAdamView Answer on Stackoverflow
Solution 4 - PhpJKirchartzView Answer on Stackoverflow