note 20087 added to function.next
| From: | nikolai at bigaction_dot_org dot chek dot com | Date: | Thu, 21 Mar 2002 23:39:09 +0000 |
| Subject: | note 20087 added to function.next | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-28055@lists.php.net to get a copy of this message | ||
> Don't try this and expect it to work:
>
> while(next($array))
>
> the loop will never begin on the first item in the array
> since next() advances the pointer BEFORE returning
> the values.
reset() returns the first element in the array, so it's not such a bad thing that next()
returns the second. To avoid the overhead of using list() with each(), you can simply do this:
$elem = reset($your_array)
do
{
//.. stuff with $elem;
} while($elem = next($your_array));
this is, of course, assuming that the array is not empty, but it's easy to wrap the whole thing
in an if().
--
http://www.php.net/manual/en/function.next.php
http://master.php.net/manage/user-notes.php?action=edit+20087
http://master.php.net/manage/user-notes.php?action=delete+20087
http://master.php.net/manage/user-notes.php?action=reject+20087