Continuing my showcasing of all of the awesomeness in PHP 5.5 that I am discovering since my upgrade from 5.3, let’s discuss using the the list() function inside of a foreach() block.
Have you ever had a situation where you are looping through a multi-dimensional array and the array is non-associative? If so, you probably have some code that looks like this:
Or even worse, you’re not even using the list() function:
Or worse than that, lacking any sort of variable mapping / meaningful references to the variables:
NO LONGER! as now you can leverage the assignment power of list() right inside of your foreach() statement:
I haven’t run any benchmarks to see if this is any faster or slower than using list() inside of the block itself instead of in the statement, but to me it’s worth a small bit of overhead for the cleanliness of the code when dealing with a small number of arguments.
What’s your preferred method? Comment below!