Last in array?
Moderator: General Moderators
Last in array?
When looping through an associative array, is there a way to find out if it's the last item in the array?
Re: Last in array?
Increment a counter throughout, then check if it's equal to count($the_array) - 1. Remember, count() starts from 1...so you have to subtract 1.
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Last in array?
Code: Select all
$lastkey = end(array_keys($arr));
foreach ($arr as $key => $value) {
if ($key == $lastkey) {
...
}
}