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, ...';
To convert an array to a comma-separated string, call the join() method on the array, passing it a string containing a comma as a parameter. The join method returns a string containing all array elements joined by the provided separator.
The task is to split the given string with comma delimiter and store the result in an array. Use explode() or preg_split() function to split the string in php with given delimiter. PHP | explode() Function: The explode() function is an inbuilt function in PHP which is used to split a string in different strings.
Use the String. split() method to convert a comma separated string to an array, e.g. const arr = str. split(',') . The split() method will split the string on each occurrence of a comma and will return an array containing the results.
Use implode:
$tags = implode(', ', array('tag1','tag2','tag3','tag4'));
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 :D )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With