| | |
foreach()
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
Hello,
I am new to php and enjoy the fact that it is very similar to languages that I'm much stronger with (mainly C++ and Java). While working through the w3 school's tutorials I noticed a foreach() loop. What loop in C++/Java would this be similar to? It seems like
but I'm not certain. What does foreach($array as $value) test for? Or is it actually performing something similar to $value = $array[i]? Thanks for helping me clear this up!
I am new to php and enjoy the fact that it is very similar to languages that I'm much stronger with (mainly C++ and Java). While working through the w3 school's tutorials I noticed a foreach() loop. What loop in C++/Java would this be similar to? It seems like
PHP Syntax (Toggle Plain Text)
while(array[i] != null) { //Do things with array[i] }
foreach($array as $value) simply means, for every element in the array, assign its value to $value.
foreach($array as $key => $value) assigns the index of the array to $key and value of that index to $value.
I hope I am not confusing you!
This is almost similar to for loop.
foreach($array as $key => $value) assigns the index of the array to $key and value of that index to $value.
I hope I am not confusing you!This is almost similar to for loop.
PHP Syntax (Toggle Plain Text)
for($i=0;$i<count($array);$i++) { echo "Key is".$i."Value is".$array[$i]; } //is same as foreach($array as $key =>$value) { echo "Key is".$key."Value is".$value; }
Last edited by nav33n; Feb 20th, 2009 at 2:06 pm.
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
No probs! Btw, foreach is useful when you have irregular array indexes.
eg.
eg.
PHP Syntax (Toggle Plain Text)
$array[3]=30; $array[10]=10; $array[11]=10;
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Apr 2008
Posts: 20
Reputation:
Solved Threads: 2
•
•
•
•
Hello,
I am new to php and enjoy the fact that it is very similar to languages that I'm much stronger with (mainly C++ and Java). While working through the w3 school's tutorials I noticed a foreach() loop. What loop in C++/Java would this be similar to?
Here's a link about Java's enhanced for loop: http://www.developer.com/java/other/article.php/3343771
•
•
•
•
What does foreach($array as $value) test for? Or is it actually performing something similar to $value = $array[i]? Thanks for helping me clear this up!
php Syntax (Toggle Plain Text)
<? $var['one']='aaa'; $var['two']='bbb'; $var['three']='ccc'; $var['four']='ddd'; $var['five']='eee'; foreach ($var AS $key => $value) { echo '$var[\''.$key."']=".$value.";<br>"; } ?>
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
•
•
•
•
foreach($array as $value) simply means, for every element in the array, assign its value to $value.
foreach($array as $key => $value) assigns the index of the array to $key and value of that index to $value.
I hope I am not confusing you!
This is almost similar to for loop.
PHP Syntax (Toggle Plain Text)
for($i=0;$i<count($array);$i++) { echo "Key is".$i."Value is".$array[$i]; } //is same as foreach($array as $key =>$value) { echo "Key is".$key."Value is".$value; }
http://www.php.net/foreach
http://www.tizag.com/phpT/foreach.php
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
![]() |
Similar Threads
- how to convert typed dataset to untyped dataset??? (C#)
- using foreach with multi-dimensional arrays (PHP)
- Multidimensional array list & foreach? (C#)
- Problem with foreach when using linking (PHP)
Other Threads in the PHP Forum
- Previous Thread: Trouble with Ajax registration form...!
- Next Thread: Chrome problem
| Thread Tools | Search this Thread |
301 apache api array autosuggest beginner binary broken cakephp checkbox class cms code compression cron curl data database date display dropdownlist dynamic echo email eregi error execution file files folder form forms function functions google href htaccess html httppost if...loop image include insert ip javascript joomla jquery key library limit link links login mail md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf pdfdownload php phpvotingscript problem query radio random recursion remote screen script search searchbox server session sessions sms sorting source space sql syntax system table tutorial update upload url validator variable video volume votedown web website youtube zend






