954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

while(array[i] != null)
{
       //Do things with array[i]
}

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!

dmanw100
Posting Whiz in Training
242 posts since Apr 2008
Reputation Points: 104
Solved Threads: 27
 

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.
:S I hope I am not confusing you!
This is almost similar to for loop.

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;
}
nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Thanks! Yeah that does make sense. I just thought it might be checking for a value instead of assigning. Thanks for clearing it up!

dmanw100
Posting Whiz in Training
242 posts since Apr 2008
Reputation Points: 104
Solved Threads: 27
 

No probs! Btw, foreach is useful when you have irregular array indexes.
eg.

$array[3]=30; $array[10]=10; $array[11]=10;
nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 
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

jedi_ralf
Newbie Poster
20 posts since Apr 2008
Reputation Points: 10
Solved Threads: 3
 
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:

<?
$var['one']='aaa';
$var['two']='bbb';
$var['three']='ccc';
$var['four']='ddd';
$var['five']='eee';
foreach ($var AS $key => $value)
    {
    echo '$var[\''.$key."']=".$value.";";
    }
?>


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.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

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. :S I hope I am not confusing you! This is almost similar to for loop.

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;
}

You can try some examples here: http://www.php.net/foreach
http://www.tizag.com/phpT/foreach.php

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You