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]
|