Php: Display Comma After Each Element Except the Last. Using 'For' Statement and No 'Implode/Explode'

PHP: Display comma after each element except the last. Using 'for' statement and no 'implode/explode'

This should work. It's better I think to call count() once rather than on every loop iteration.

$count = count($director);
for ($i = 0; $i < $count; $i++) {
echo '<a href="person.php?id='.$director[$i]["id"].'">'.$director[$i]["name"].'</a>';

if ($i < ($count - 1)) {
echo ', ';

PHP : Add Comma after every Word (Except Final)

Try this:

$newstring = implode(", ", preg_split("/[\s]+/", $oldstring));

The preg_split() will split up your string into an array and the implode() will collapse it all back together into a single string. The regex used in the preg_split() will take care of any instances you might have multiple spaces between words.

How can I add a comma after each array item?

An alternative method to looping would be to implode and explode the array.

Implode on comma and some other character, then explode on that other character.

$arr = explode("@", implode(",@", $arr));

Comma separated list from array with and before last element

Try array_pop() to get the last element, then array_push to modify and push the element back:

Something like

$last_element = array_pop($number_list);
array_push($number_list, 'and '.$last_element);

Then you can do your implode:

$comma_list = implode(', ', $number_list);

print commas after each print statement except the last one

The trick is to print the comma on all the iterations but the first one (that is, to reverse your logic):

set xs {a b c}
set s ""
set need_comma false
foreach x $xs {
if {$need_comma} {
append s ,
} else {
set need_comma true
append s $x
puts $s

would produce


Note that it seems you're merely looking for the join command.

How to remove last character (comma when creating a string from an array)?

Just keep it simple and use implode(), e.g.


$images = "image1,image2,image3";
$arr = explode(",", $images);
echo "'" . implode("','", $arr) . "'";




How to implode comma separated outside string

You just need to add the single quotes into your implode glue string, and at the outsides of the result string:

$array = [1, 2, 3];
echo "'" . implode("','", $array) . "'";



This will work regardless of whether your array values are strings or numbers e.g.

$array = ['1', '2', '3'];
echo "'" . implode("','", $array) . "'";



Demo on

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

You want to use implode for this.

$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.

Remove Last Comma From Array Value in Loop

Explode on a single comma, use array_filter to remove empty entries, and implode to rejoin the values:

public function unserializeArray($string) {
return array_values(array_filter(explode(',' $string)));
echo implode(',',$sw::shared()->users->unserializeArray($specialties));

Related Topics

Leave a reply