| | |
foreach()
![]() |
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 1: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 topics as it can be really annoying.
http://syntax.cwarn23.net/
My favourite PC. -- Jumba webhosting
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*` Want 1 on 1 php help, add me to msn at msn@cwarn23.infoMy favourite PC. -- Jumba webhosting
•
•
•
•
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?
"Try to be expert in everything and you become expert in nothing" - Me
---------------------------------
Windows Vista Home Premium SP2| MINGW 4.4.0|PHP 5.3.1|Python 2.6.4|JDK 1.6
Theist: It's okay, can you imagine anything else that doesn't exist?
"Try to be expert in everything and you become expert in nothing" - Me
---------------------------------
Windows Vista Home Premium SP2| MINGW 4.4.0|PHP 5.3.1|Python 2.6.4|JDK 1.6
![]() |
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
Views: 655 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for PHP
access ajax apache array beginner binary box buttons cakephp check checkbox class cms code cookies css database date delete directory display download dropdown drupal dynamic echo email error file files form forms functions header href htaccess html image images include insert ip java javascript joomla jquery limit link login lookup loop mail menu mlm mod_rewrite multiple mysql onclick order parse password paypal php post problem query radio redirect regex remote results rewrite script search security select server session sessions soap sort source sql storage string system table update upload url user validate validation validator variable video web website wordpress xml






