Page 1 of 1

Foreach Loop Help v.newtophp

Posted: Mon Mar 13, 2006 11:20 pm
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!

Posted: Mon Mar 13, 2006 11:28 pm
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.

Posted: Tue Mar 14, 2006 3:09 pm
by nubian
thank you