How do I create a comma separated list from an array in PHP?

I know how to iterate over array elements using foreach and add a comma, but it is always difficult to get rid of the last comma. Is there an easy way to do this in PHP?

$fruit = array('apple', 'banana', 'pear', 'grape'); 

I want to end up

 $result = "apple, banana, pear, grape" 
+100
arrays php
Mar 12 '10 at 19:23
source share
11 answers

You want to use implode for this.

namely: $commaList = implode(', ', $fruit);




There is a way to add commas without trailing. You want to do this if you have to do some other manipulations at the same time. For example, perhaps you want to quote each fruit and then separate them with commas:

 $prefix = $fruitList = ''; foreach ($fruits as $fruit) { $fruitList .= $prefix . '"' . $fruit . '"'; $prefix = ', '; } 

Also, if you just make it the β€œnormal” way to add a comma after each item (as if it sounds like you did before), and you need to trim the last one, just $list = rtrim($list, ', ') . In this situation, I see a lot of people who unnecessarily dump using substr .

+211
Mar 12 '10 at 19:24
source share

Here is how I did 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: In @joseantgv's comment, you can remove rtrim() from the above example. I.e:

 $string = implode(',', $arr); 
+35
Jan 11 '14 at 18:09
source share

Result with and at 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 
+5
Oct 22 '14 at 7:15
source share

I prefer to use the IF statement in a FOR loop, which checks that the current iteration is not 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 ", "; } } 
+3
Nov 08
source share

Like Lloyd, but works with any array size.

 $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." } 
+2
Feb 05 '14 at
source share

Sometimes you don’t even need php for this in certain cases (for example, list items in your own rendering tag). You can always add commas to all elements, but last-child via css if they are separate elements after rendering from the script.

I use this a lot in basic applications to trim some arbitrary code:

 .likers a:not(:last-child):after { content: ","; } 

It basically searches for an element, targets everything except the last element, and adds a comma after each element. Just an alternative way to not use a script at all if the case applies.

+1
Mar 13. '14 at 18:07
source share
 $fruit = array('apple', 'banana', 'pear', 'grape'); $commasaprated = implode(',' , $fruit); 
0
Nov 02 '16 at 10:40
source share

A functional solution will look like this:

 $fruit = array('apple', 'banana', 'pear', 'grape'); $sep = ','; array_reduce( $fruits, function($fruitsStr, $fruit) use ($sep) { return (('' == $fruitsStr) ? $fruit : $fruitsStr . $sep . $fruit); }, '' ); 
0
Feb 10 '17 at 13:21
source share

If you make quoted answers, you can do

 $commaList = '"'.implode( '" , " ', $fruit). '"'; 

the above assumes that the fruits are not equal to zero. If you do not want to make this assumption, you can use the if-then-else operator or the triple (? :) operator.

-one
Jan 27 '14 at 11:41
source share

Another way could be this:

 $letters = array("a", "b", "c", "d", "e", "f", "g"); $result = substr(implode(", ", $letters), 0, -3); 

Output $result is a perfectly formatted comma separated list.

 a, b, c, d, e, f, g 
-2
Jan 28 '14 at 4:13
source share
 $letters = array("a", "b", "c", "d", "e", "f", "g"); // this array can n no. of values $result = substr(implode(", ", $letters), 0); echo $result 

output-> a, b, c, d, e, f, g

-2
Apr 22 '14 at 8:17
source share



All Articles