Wondering what would be a good method to get the first iteration on a foreach loop. I want to do something different on the first iteration.
Is a conditional our best option on these cases?
Wondering what would be a good method to get the first iteration on a foreach loop. I want to do something different on the first iteration.
Is a conditional our best option on these cases?
You can simply add a counter to the start, like so:
This is also works
Yes, if you are not able to go through the object in a different way (a normal for loop), just use a conditional in this case:
Even morer eleganterer:
That example only works if you use PHP's built-in array append features/function or manually specify keys in proper numerical order.
Here's an approach that is not like the others listed here that should work via the natural order of any PHP array.