Re: end of array
| From: | Rasmus Lerdorf | Date: | Sat, 22 Jan 2005 16:46:59 +0000 |
| Subject: | Re: end of array | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-207043@lists.php.net to get a copy of this message | ||
Jeffery Fernandez wrote:
Hi all, I have a foreach loop on an array and within that loop I need to find if the array has reached the last pointer. I have tried if (next($row)) { } but that advances the pointer. Any tips on finding out if the array pointer has reached the last element ?end($arr); $last = key($arr); foreach($arr as $key=>$elem) { if($key !== $last) {
...} } That would do exactly what you asked, however, it sounds like if you want to do something for every item in the array except the last you should just remove that last item before your loop. $last = array_pop($arr); foreach($arr as $elem) { ... } -Rasmus