Reply to Re: While loop in array does not start with first entry

Your name:

Reply:


Posted by Janwillem Borleffs on 06/05/05 23:46

Matthias Braun wrote:
> Help!
>
> I am using arrays and a while loop for go through all the elements.
> The problem is that the first element is missing:
>
> while ($val = next($array))
> {
> $key = key($array);
> echo "$key: $val\n"
>
> }

next() will always get the next element by moving the pointer ahead. To get
the first element, use current():

$val = current($array);
while ($val !== false) {
$key = key($array);
echo "$key: $val\n";
$val = next($array);
}

Or use list() with each():

while (list($key, $val) = each($array)) {
echo "$key: $val\n";
}

Or even better, use foreach as Geoff mentioned.


JW

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация