Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

While loop in array does not start with first entry
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"

}

Report this thread to moderator Post Follow-up to this message
Old Post
Matthias Braun
06-05-05 08:56 PM


Re: While loop in array does not start with first entry
Matthias Braun said the following on 05/06/2005 18:51:
> 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"
>
> }

reset($array);


--
Oli

Report this thread to moderator Post Follow-up to this message
Old Post
Oli Filth
06-05-05 08:56 PM


Re: While loop in array does not start with first entry
I noticed that Message-ID:
<42a33bbd$0$14739$9b4e6d93@newsread4.arcor-online.net> from Matthias
Braun contained the following:

>
>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"
>
>}

That's what foreach is for

foreach ($array as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}


--
Geoff Berrow  0110001001101100010000000110
 0011011010110110010001101111011001110010
11
 1001100011011011110010111001110101011010
11

Report this thread to moderator Post Follow-up to this message
Old Post
Geoff Berrow
06-06-05 01:55 AM


Re: While loop in array does not start with first entry
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




Report this thread to moderator Post Follow-up to this message
Old Post
Janwillem Borleffs
06-06-05 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:41 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.