Skip to content
Advertisement

Removing last comma in array when looping without turning it into a string

I have a set of tags that I stored in the database separated by spaces as a string. When I echo them out with PHP, I explode them and loop through them with a for loop.

My code looks something like this:

JavaScript

So far I’ve only seen posts with answers to use implode, but if I use implode, I wouldn’t be able to click on the individual tags because it would be a string..

So I’m trying to figure out how to get rid of the last comma when I’m looping the tags array for each tag. It would ultimately appear like this:

JavaScript

Advertisement

Answer

You could just change the comma to the beginning of the echo in the for loop and implement a condition to check if its the first tag… like this:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement