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.
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.
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.
Definition and UsageThe implode() function returns a string from the elements of an array. Note: The implode() function accept its parameters in either order. However, for consistency with explode(), you should use the documented order of arguments. Note: The separator parameter of implode() is optional.
You want to use implode for this.
ie:
$commaList = implode(', ', $fruit);
There is a way to append commas without having a trailing one. You'd want to do this if you have to do some other manipulation at the same time. For example, maybe you want to quote each fruit and then separate them all by commas:
$prefix = $fruitList = '';
foreach ($fruits as $fruit)
{
$fruitList .= $prefix . '"' . $fruit . '"';
$prefix = ', ';
}
Also, if you just do it the "normal" way of appending a comma after each item (like it sounds you were doing before), and you need to trim the last one off, just do $list = rtrim($list, ', ')
. I see a lot of people unnecessarily mucking around with substr
in this situation.
This is how I've been doing it:
$arr = array(1,2,3,4,5,6,7,8,9);
$string = rtrim(implode(',', $arr), ',');
echo $string;
Output:
1,2,3,4,5,6,7,8,9
Live Demo: http://ideone.com/EWK1XR
EDIT: Per @joseantgv's comment, you should be able to remove rtrim()
from the above example. I.e:
$string = implode(',', $arr);
Result with and
in the end:
$titleString = array('apple', 'banana', 'pear', 'grape');
$totalTitles = count($titleString);
if ($totalTitles>1) {
$titleString = implode(', ', array_slice($titleString, 0, $totalTitles-1)) . ' and ' . end($titleString);
} else {
$titleString = implode(', ', $titleString);
}
echo $titleString; // apple, banana, pear and grape
Similar to Lloyd's answer, but works with any size array.
$missing = array();
$missing[] = 'name';
$missing[] = 'zipcode';
$missing[] = 'phone';
if( is_array($missing) && count($missing) > 0 )
{
$result = '';
$total = count($missing) - 1;
for($i = 0; $i <= $total; $i++)
{
if($i == $total && $total > 0)
$result .= "and ";
$result .= $missing[$i];
if($i < $total)
$result .= ", ";
}
echo 'You need to provide your '.$result.'.';
// Echos "You need to provide your name, zipcode, and phone."
}
I prefer to use an IF statement in the FOR loop that checks to make sure the current iteration isn't the last value in the array. If not, add a comma
$fruit = array("apple", "banana", "pear", "grape");
for($i = 0; $i < count($fruit); $i++){
echo "$fruit[$i]";
if($i < (count($fruit) -1)){
echo ", ";
}
}
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