Page 1 of 1

using the "foreach" statement in arrays: WHY?

Posted: Mon Jun 23, 2003 2:22 pm
by HungryMind
I am reading through a book and I have come to this part that says:

Code: Select all

You can use the "foreach" statement to create a loop that allows you to work with each consecutive element in an array without having to specify each element's index. In the following example, each element in the $ vegetables array is assigned to the $value variable.
The example in the book is as follows:

Code: Select all

$vegetables = array ("broccoli", "carrot", "zucchini", "eggplant");
foreach ($vegetables as $value)
{
        echo "The vegetable is: $value<br />\n";
}
I am having a hard time with the statement "$vegetables as $value". How would one go about reading this statement, what is "foreach" doing to the variables $vegetables and $value and what is the "as" statement and how does it work?

EDIT: ONE MORE QUESTION! :wink:

The book also says that if I want to access all of the varaibles in an array to type the following code:

Code: Select all

$paredes_family = array ("Frank Paredes", "Janice Paredes", "Brandon Paredes", "Jake Paredes");

for ($counter = 0; $counter < count($paredes_family); $counter++)
{
        echo "$paredes_family[$counter]<br />\n";
}
That seems like a lot of added steps that could be done like this instead:

Code: Select all

$paredes_family = array ("Frank Paredes", "Janice Paredes", "Brandon Paredes", "Jake Paredes");

foreach ($paredes_family as $fam)
{
        echo "$fam<br />\n";
}
And so, my second question is: What kind of benefits or uses would there be by accessing the variables using the first method rather than the latter?

Posted: Mon Jun 23, 2003 3:10 pm
by m@ndio
Hi,

Let me explain, correct me if I am wrong anyone please.

Code: Select all

$vegetables = array ("broccoli", "carrot", "zucchini", "eggplant"); 
foreach ($vegetables as $value) 
{ 
        echo "The vegetable is: $value<br />\n"; 
}
1. Ok first of all you set up the array called vegetables with 4 elements inside..

2. This is how I read the foreach statement in my head:

"for each element in the vegetables array store the element in the variable $value"

This line will loop through the entire array storing a different element of the array in the variable $value each time. So the out put prints the following:

The vegetable is: broccoli
The vegetable is: carrot
The vegetable is: zucchini
The vegetable is: eggplant

does this help you I hope I explained it well..

Posted: Mon Jun 23, 2003 3:15 pm
by HungryMind
m@ndio wrote:"for each element in the vegetables array store the element in the variable $value"
Yes, that is EXACTLY what I was looking for to answer my first question!!! Thanks. My second question still stands however. I shall repeat it so you guys don't have to scroll up again :wink: .
HungryMind wrote:The book also says that if I want to access all of the varaibles in an array to type the following code:

Code: Select all

$paredes_family = array ("Frank Paredes", "Janice Paredes", "Brandon Paredes", "Jake Paredes");

for ($counter = 0; $counter < count($paredes_family); $counter++)
{
        echo "$paredes_family[$counter]<br />\n";
}
That seems like a lot of added steps that could be done like this instead:

Code: Select all

$paredes_family = array ("Frank Paredes", "Janice Paredes", "Brandon Paredes", "Jake Paredes");

foreach ($paredes_family as $fam)
{
        echo "$fam<br />\n";
}
And so, my second question is: What kind of benefits (if any) or uses would there be by accessing the variables using the first method rather than the latter?

Posted: Mon Jun 23, 2003 3:21 pm
by nielsene
Well the for method feels more natural to C/C++ programmers, while the foreach method may not be as apparent. The for method is much more flexible in general and often poeple just use it, or the even more general while-loop, all the day. foreach is nice syntactic sugar. Its not needed in a languange, but can make things look a lot nicer...

I know I should probably use foreach more, but I'm so used to for loops, plus php didn't have foreach when i started using it....

Posted: Mon Jun 23, 2003 3:21 pm
by Galahad
Here's the manual page for foreach.

Here's my summary of what it does:
Let's say you have an array like this:

Code: Select all

$array&#1111;'0'] = "first";
$array&#1111;'1'] = "second";
$array&#1111;'2'] = "third";
Arrays are structured like $array[$key] = $value. So in this example, where the key is '1' the value is "second".

So when you do a foreach, it steps through each key in an array.

"foreach ($array as $v)" means step through each key in $array and let me access the current key's value as $v. If I did

Code: Select all

<?php
foreach ($array as $v) {
  echo "The value for this key is: $v<br />\n";
}
?>
The output would look like:
The value for this key is: first
The value for this key is: second
The value for this key is: third
You can also do:

Code: Select all

<?php
foreach ($array as $k => $v) {
  echo "The value for $k is: $v<br />\n";
}
?>
That would output:
The value for 0 is: first
The value for 1 is: second
The value for 2 is: third
That does essentially the same thing, but it also give you a way to access which is the current key.

I hope that is clear and somewhat helpful. Ok, this was already answered, I type too slowly.

Posted: Mon Jun 23, 2003 3:25 pm
by m@ndio
I dont really see any difference to using a for loop or the foreach statement to cycle through array elements..

I guess it's just personal preference.

Posted: Mon Jun 23, 2003 3:39 pm
by HungryMind
That really helps, but let me ask one more finalizing question concerning foreach and the for loops. Basically, what I want to know is: Is there a reason why I should use one or the other? Obviously, it will be helpful to keep both in mind for analyzing other people's work, but are there drawbacks for each and are there options that come with one that the other cannot do (as easily or at all)?

EDIT: OOps, you answered my question while I was posting.

Thanks for all of your help, this really cleared up a lot of issues for me! I sure do wish authors would take the time to answer these kind of issues in their books. Thanks again!