How to convert items in array to a comma separated string in PHP?

PhpArrays

Php Problem Overview


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

Given this array:

$tags = array('tag1','tag2','tag3','tag4','...');

How do I generate this string (using PHP):

$tags = 'tag1, tag2, tag3, tag4, ...';

Php Solutions


Solution 1 - Php

Use implode:

 $tags = implode(', ', array('tag1','tag2','tag3','tag4'));

Solution 2 - Php

Yes you can do this by using [implode][1]

$string = implode(', ', $tags);

And just so you know, there is an alias of implode, called [join][2]

$string = join(', ', $tags);

I tend to use join more than implode as it has a better name (a more self-explanatory name :D )

[1]: http://uk.php.net/manual/en/function.implode.php "implode" [2]: http://uk.php.net/manual/en/function.join.php

Solution 3 - Php

Use PHP function Implode on your array

$mystring = implode(', ',$tags)

Solution 4 - Php

Simply implode:

$tags = implode(", ", $tags);

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
QuestionAhmed FouadView Question on Stackoverflow
Solution 1 - PhpJohn CondeView Answer on Stackoverflow
Solution 2 - PhpRutZapView Answer on Stackoverflow
Solution 3 - PhpGDPView Answer on Stackoverflow
Solution 4 - PhpMike MackintoshView Answer on Stackoverflow