Home » Php » How to convert items in array to a comma separated string in PHP? [duplicate]

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

Posted by: admin November 30, 2017 Leave a comment

Questions:

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, ...';
Answers:

Use implode:

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

Questions:
Answers:

Yes you can do this by using implode

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

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

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

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

Questions:
Answers:

Use PHP function Implode on your array

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

Questions:
Answers:

Simply implode:

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