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? Thanks for helping me clear this up!

Recommended Answers

All 6 Replies

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

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

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

$array[3]=30; $array[10]=10; $array[11]=10;

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

What does foreach($array as $value) test for? Or is it actually performing something similar to $value = $array? 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.";<br>";
    }
?>

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.