foreach()

Reply

Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

foreach()

 
0
  #1
Feb 20th, 2009
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
  1. while(array[i] != null)
  2. {
  3. //Do things with array[i]
  4. }
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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 331
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: foreach()

 
0
  #2
Feb 20th, 2009
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.
  1. for($i=0;$i<count($array);$i++) {
  2. echo "Key is".$i."Value is".$array[$i];
  3. }
  4. //is same as
  5. foreach($array as $key =>$value) {
  6. echo "Key is".$key."Value is".$value;
  7. }
Last edited by nav33n; Feb 20th, 2009 at 2:06 pm.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: foreach()

 
0
  #3
Feb 20th, 2009
Thanks! Yeah that does make sense. I just thought it might be checking for a value instead of assigning. Thanks for clearing it up!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 331
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: foreach()

 
0
  #4
Feb 20th, 2009
No probs! Btw, foreach is useful when you have irregular array indexes.
eg.
  1. $array[3]=30; $array[10]=10; $array[11]=10;
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 20
Reputation: jedi_ralf is an unknown quantity at this point 
Solved Threads: 2
jedi_ralf jedi_ralf is offline Offline
Newbie Poster

Re: foreach()

 
0
  #5
Feb 20th, 2009
Originally Posted by dmanw100 View Post
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's similar to Java's enhanced for loop when using an iterator over a collection of objects. It's also called a foreach loop although uses the syntax "for".
Here's a link about Java's enhanced for loop: http://www.developer.com/java/other/article.php/3343771
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,453
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is online now Online
Nearly a Posting Virtuoso

Re: foreach()

 
0
  #6
Feb 21st, 2009
Originally Posted by dmanw100 View Post
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!
Below is an example that will display all of the arrays and their values in the php format:
  1. <?
  2. $var['one']='aaa';
  3. $var['two']='bbb';
  4. $var['three']='ccc';
  5. $var['four']='ddd';
  6. $var['five']='eee';
  7. foreach ($var AS $key => $value)
  8. {
  9. echo '$var[\''.$key."']=".$value.";<br>";
  10. }
  11. ?>
So what foreach basically does is loop through the array one array value at a time and assign the array value the the variable $value (in the example above) and the key (between the [] brackets) to the $key variable as shown in my example. Then those variables can be used in the loop while looping each value+key one at a time.
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,357
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: foreach()

 
0
  #7
Feb 22nd, 2009
Originally Posted by nav33n View Post
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.
  1. for($i=0;$i<count($array);$i++) {
  2. echo "Key is".$i."Value is".$array[$i];
  3. }
  4. //is same as
  5. foreach($array as $key =>$value) {
  6. echo "Key is".$key."Value is".$value;
  7. }
You can try some examples here:
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC