943,712 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1228
  • PHP RSS
Feb 20th, 2009
0

foreach()

Expand 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 seems like
PHP Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008
Feb 20th, 2009
0

Re: foreach()

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)
  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.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Feb 20th, 2009
0

Re: foreach()

Thanks! Yeah that does make sense. I just thought it might be checking for a value instead of assigning. Thanks for clearing it up!
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008
Feb 20th, 2009
0

Re: foreach()

No probs! Btw, foreach is useful when you have irregular array indexes.
eg.
PHP Syntax (Toggle Plain Text)
  1. $array[3]=30; $array[10]=10; $array[11]=10;
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Feb 20th, 2009
0

Re: foreach()

Click to Expand / Collapse  Quote originally posted by dmanw100 ...
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
Reputation Points: 10
Solved Threads: 3
Newbie Poster
jedi_ralf is offline Offline
20 posts
since Apr 2008
Feb 21st, 2009
0

Re: foreach()

Click to Expand / Collapse  Quote originally posted by dmanw100 ...
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:
php Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Feb 22nd, 2009
0

Re: foreach()

Click to Expand / Collapse  Quote originally posted by nav33n ...
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)
  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
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Trouble with Ajax registration form...!
Next Thread in PHP Forum Timeline: Chrome problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC