How to remove comma at the end of an array?
Need output: 1,2,3,4,5 and not 1,2,3,4, 5,
I know I can use implode, but is there another way?
$massive = [1, 2, 3, 4, 5];
foreach($massive as $items){
echo $items. ', ';
}
How to remove comma at the end of an array?
Need output: 1,2,3,4,5 and not 1,2,3,4, 5,
I know I can use implode, but is there another way?
$massive = [1, 2, 3, 4, 5];
foreach($massive as $items){
echo $items. ', ';
}
2
Answers
You can use implode and if don’t want to use the inbuilt function then use a new variable like this
This pattern shows up frequently. I thought I might offer a generic way to handle it.
What if I wanted to output the data as the loop progresses?
Just set a boolean to false then after processing the first element, set it to true.
Is there another way? Yes.
Or we can detect the last item and just not output the comma.