find last iteration of a foreach loop on an array
Posted: Fri Aug 18, 2006 1:17 am
Hey all,
This seems like such a newb question but I'm looking through some old code and I cant believe I came to doing something the way I did ... and what's worse, I cant seem to see a better way.
I generate an array with a list of values, which print_r shows as:
When setting the array (from a database) I just set each index to a variable called size_last so that the last one added is marked as the last in the array (the query returns the values with keys sorted).
Then later on, in order to find the last one, I just do:
Seems pretty messy, but I cant seem to find a way to tell it to do something different on the final run through. Surely there is a built-in function for this somewhere.
Any tips would be great. Cheers
This seems like such a newb question but I'm looking through some old code and I cant believe I came to doing something the way I did ... and what's worse, I cant seem to see a better way.
I generate an array with a list of values, which print_r shows as:
Code: Select all
array( [32]=> Small [34]=> Medium [46]=> Large )Then later on, in order to find the last one, I just do:
Code: Select all
foreach($size_list as $key=>$size_name){
if($key == $size_last) ... do suff
}Any tips would be great. Cheers
