Edgar Edgar - 1 month ago 13
PHP Question

Php foreach - seperating 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)

Answer

Try this:

echo implode(",<br/>", $array);
Comments