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));
https://3v4l.org/mXuPl
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:
http://php.net/manual/en/function.array-pop.php
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
a,b,c
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.
<?php
$images = "image1,image2,image3";
$arr = explode(",", $images);
echo "'" . implode("','", $arr) . "'";
?>
output:
'image1','image2','image3'
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) . "'";
Output:
'1','2','3'
This will work regardless of whether your array values are strings or numbers e.g.
$array = ['1', '2', '3'];
echo "'" . implode("','", $array) . "'";
Output:
'1','2','3'
Demo on 3v4l.org
How do I create a comma-separated list from an array in PHP?
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.
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
Implode a Column of Values from a Two Dimensional Array
How to Parameterize the Table Name in a Prepared Statement
How to Send a Post Request With PHP
Php: Fastest Way to Handle Undefined Array Key
How to Remove the Extra Square Bracket from Json Having Multiple Arrays
Install PHP Intl Extension on Macos
What Does the PHP Error Message "Notice: Use of Undefined Constant" Mean
Pdo Support For Multiple Queries (Pdo_MySQL, Pdo_MySQLnd)
Php Substring Remove Everything in a String After Certain Character
How to Get Multiple Value from Post Variable Using Same Name
Get Image Type from Base64 Encoded Src String
PHP - Using Pdo With in Clause Array
Showing All Errors and Warnings
How Does PHP 'Foreach' Actually Work
Chrome Net::Err_Incomplete_Chunked_Encoding Error