Skip to content
Advertisement

Php foreach – separating each loop with comma except the last element

Heres what i mean:

foreach ($array as $a) {
 echo $a.',<br/>';
}

current output would be:

a,
a,
a,
a,

i want the output to be like this:

a,
a,
a,
a

(all ‘a’ separated with comma and when it comes to the last loop it doesnt write a comma)

Advertisement

Answer

Try this:

echo implode(",<br/>", $array);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement