Foreach Loop Help v.newtophp

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nubian
Forum Newbie
Posts: 9
Joined: Wed Dec 08, 2004 7:32 pm

Foreach Loop Help v.newtophp

Post by nubian »

i'm currently reading this book on php and i'm stuck on why the code is the way it is.
for example:

Code: Select all

$user['firstname'] = 'jack';
$user['lastname'] = 'doe';
$user['age'] = '24';

foreach ($array as $key => $value) {
statements
}
what i don't understand is what specifically in detail $array as $key is doing.
any help will be greatly appreciated.
thank you!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

foreach iterates over an array where it breaks out the keys (indice names) or each element and the value (depending on how you call it)


foreach( expression as index variable => value variable ) statement block
each time the statement block is entered, index variable and value variable will be the next available index and value in the array.


foreach( expression as value variable ) statement block
each time the statement block is entered, value variable will be the next available value in the array.
nubian
Forum Newbie
Posts: 9
Joined: Wed Dec 08, 2004 7:32 pm

Post by nubian »

thank you
Post Reply