how does foreach loop work php? - php

for example:
$numbers = array(1, 2, 3, 4, 5);
foreach($numbers as $value)
{
echo $value;
}
what does as do, I assume it's a keyword because it is highlighted as one in my text editor. I checked the keyword list at http://www.php.net/manual/en/reserved.keywords.php and the as keyword was present. It linked to the foreach construct page, and from what I could tell didn't describe what the as keyword did. Does this as keyword have other uses or is it just used in the foreach construct?

It's used to iterate over Iterators, e.g. arrays. It reads:
foreach value in $numbers, assign the value to $value.
You can also do:
foreach ($numbers as $index => $value) {
}
Which reads:
foreach value in $numbers, assign the index/key to $index and the value to $value.
To demonstrate the full syntax, lets say we have the following:
$array = array(
0 => 'hello',
1 => 'world')
);
For the first iteration of our foreach loop, $index would be 0 and $value would be 'hello'. For the second iteration of our foreach loop, $index would be 1, and $value would be 'world'.
Foreach loops are very common. You may have seen them as for..in loops in other languages.
For the as keyword, you're saying:
for each value in $numbers AS $value
It's a bit awkward, I know. As far as I know, the as keyword is only used with foreach loops.

as is just part of the foreach syntax, it's used to specify the variables that are assigned during the iteration. I don't think it's used in any other context in PHP.

Related

foreach loop and reference of &$value

Why is an empty foreach loop can change the result.
I have the following code:
$variable = [1,2,3,4];
foreach ($variable as $key => &$value)
$value ++;
var_dump($variable);
The result I get is:
array (size=4)
0 => int 2
1 => int 3
2 => int 4
3 => &int 5
Now, when I add an empty foreach loop like this:
$variable = [1,2,3,4];
foreach ($variable as $key => &$value)
$value ++;
foreach ($variable as $key => $value);
var_dump($variable);
I get this :
array (size=4)
0 => int 2
1 => int 3
2 => int 4
3 => &int 4
can someone explain me why the last element doesn't change when I add the second empty loop, and why there is a & infront of the last element?
At the end of the first loop, $value is pointing to the same place as $variable[3] (they are pointing to the same location in memory):
$variable = [1,2,3,4];
foreach ($variable as $key => &$value)
$value ++;
Even as this loop is finished, $value is still a reference that's pointing to the same location in memory as $variable[3], so each time you store a value in $value, this also overwrites the value stored for $variable[3]:
foreach ($variable as $key => $value);
var_dump($variable);
With each evaluation of this foreach, both $value and $variable[3] are becoming equal to the value of the iterable item in $variable.
So in the 3rd iteration of the second loop, $value and $variable[3] become equal to 4 by reference, then during the 4th and final iteration of the second loop, nothing changes because you're passing the value of $variable[3] (which is still &$value) to $value (which is still &$value).
It's very confusing, but it's not even slightly idiosyncratic; it's the code executing exactly as it should.
More info here: PHP: Passing by Reference
To prevent this behavior it is sufficient to add an unset($value); statement after each loop where it is used. An alternative to the unset may be to enclose the foreach loop in a self calling closure, in order to force $value to be local, but the amount of additional characters needed to do that is bigger than just unsetting it:
(function($variable){
foreach ($variable as $key => &$value) $value++;
})($variable);
This is a name collision: the name $value introduced in the first loop exists after it and is used in the second loop. So all assignments to it are in fact assignments to the original array. What you did is easier observed in this code:
$variable = [1,2,3,4];
foreach ($variable as $key => &$value)
$value ++;
$value = 123; // <= here you alter the array!
var_dump($variable);
and you will see $variable[3] as 123.
One way to avoid this is, as others said, to unset ($value) after the loop, which should be a good practice as recommended by the manual. Another way is to use another variable in the second loop:
$variable = [1,2,3,4];
foreach ($variable as $key => &$value)
$value ++;
foreach ($variable as $key => $val);
var_dump($variable);
which does not alter your array.
The last element of the array will remian even after the foreach loop ..So its needed to use unset function outside the loop ..That is
$variable = [1,2,3,4];
foreach ($variable as $key => &$value) {
$value++;
}
unset($value);
var_dump($variable);
The link to the manual can be found here http://php.net/manual/en/control-structures.foreach.php
As phil stated in the comments:
As mentioned in the manual, you should unset() references after use.
$variable = [1,2,3,4];
foreach ($variable as $key => &$value) {
$value ++;
}
unset($value);
foreach ($variable as $key => $value);
print_r($variable);
Will return:
Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
)
Example
Explanation
Taken from the foreach() manual. (See the big red box)
Reference of a $value and the last array element remain even after the
foreach loop. It is recommended to destroy it by unset().
It basically means: That the referenced value &$value and the last element/item in the array, which in this case is 4 remain the same. To counter-act this issue, you'll have to unset() the value after use, otherwise it will stay in the array as its original value (if that makes sense).
You should also read this: How does PHP 'foreach' actually work?
After loop you should unset this reference using:
unset($value);
So your whole code should work like this:
$variable = [1,2,3,4];
foreach ($variable as $key => &$value) {
$value++;
}
unset($value);
var_dump($variable);
There is no point to put unset($value); inside the loop
Explanation - after loop $value is still set to the last element of array so you can use after your loop $value = 10; (before unset) and you will see that last element of array has been changed to 10. It seems that var_dump want to help us a bit in this case and shows there is reference for last element and of course when we use unset we have desired output of var_dump.
You could also look at the following script:
<?php
$array = [1, 2, 3, 4];
var_dump($array);
$x = &$array[2];
var_dump($array);
$x += 20;
unset($x);
var_dump($array);
?>
We don't use loop here and if reference is set to element of array, var_dump shows us this putting & before type of this element.
However if the above code we changed reference and set it this way $x = &$array; var_dump wouldn't show us any reference.
Also in the following code:
<?php
$x = 23;
$ref = &$x;
var_dump($x);
?>
var_dump() won't give us any hint.
Obligatory statement: References are evil!
Stepping through your code:
$variable = [1,2,3,4];
foreach ($variable as $key => &$value)
$value++;
After the loop completes; $value is a reference to $variable[3] and thus has the value of int(4).
foreach ($variable as $key => $value);
At each iteration, $variable[3] gets assigned an element of $variable[<k>] where 0 <= k < 3. At the last iteration it gets assigned to its own value which is that of the previous iteration, so it's int(4).
Unsetting $value in between the two loops resolves the situation. See also an earlier answer by me.

PHP foreach in English

This may sound weird but since not an English native, I'm having a trouble grasping the semantics of 'as' in the code.
$array = array(1, 2, 3, 4);{
foreach ($array as &$value)
$value = $value * 2;}
I don't get the meaning of as. I get what the command line is producing, but it's confusing for me probably as some of this would be for you:
foreach ($array with &$value)
or
foreach ($array then &$value)
Can some native Englishman/lady explain me why was 'as' chosen for this purpose.
It means something like
foreach item in $array provide it to me as variable $value
Just look it up in the wiktionary:
In the manner or role specified.
The kidnappers released him as agreed. The parties were seen as agreeing on a range of issues. He was never seen as the boss, but rather as a friend.
So in the context of foreach it means: go through every element of the array and use it as the specified variable (foreach($array as $content)).
For example, if there are 3 items in $array with numeric index, then the as means each time through the loop $value equals the current item, so you can think of it as an alias for the current item:
//first iteration
$value = $array[0]
//second
$value = $array[1]
//third
$value = $array[2]
It is the same as:
for($key=0; $key<count($array); $key++) {
$value = $array[$key];
}
foreach($array as $key => $value) { }

How to implement loop and calculate the value in php

As a php beginner, I meet a problem with calculating the elements of array in php
$effect=array("a"=>array(1,2),"b"=>array(1,2),"c"=>array(1,2));
I just want to make the result as this
$effect['a'][0]=$effect['a'][0]/$effect['a'][1];
$effect['b'][0]=$effect['b'][0]/$effect['b'][1];
$effect['c'][0]=$effect['c'][0]/$effect['c'][1];
Except do this one by one , How to do this calculation with foreach or other loop way
Your array syntax is a bit off. It should be $effect['a'][0].
The loop is trivial, and foreach was the right idea.
You can use it to iterate over all the letters using:
foreach ($effect as $letter => $numbers) {
...
}
Then put your assignment/division line in the loop, replacing the fixed 'a' and 'b' etc. with the $letter variable.
You need something like this?
foreach ($effect as $key => $val)
{
$results[$key] = $val[0] / $val[1];
}
print_r($results);
Also one counter-intuitive thing in PHP, is that arrays are passed by value by default. You can use & to get a reference to the array
$effects =array("a"=>array(1,2),"b"=>array(1,2),"c"=>array(1,2));
foreach ( $effects as $key => &$effect ) {
$effect[0] = $effect[0]/$effect[1];
unset($effect);
}
print_r( $effects );

How to get Array index in foreach

I have a foreach loop in php to iterate an associative array. Within the loop, instead of increamenting a variable I want to get numeric index of current element. Is it possible.
$arr = array('name'=>'My name','creditcard'=>'234343435355','ssn'=>1450);
foreach($arr as $person){
// want index here
}
I usually do this to get index:
$arr = array('name'=>'My name','creditcard'=>'234343435355','ssn'=>1450);
$counter =0;
foreach($arr as $person){
// do a stuff
$counter++;
}
Use this syntax to foreach to access the key (as $index) and the value (as $person)
foreach ($arr as $index => $person) {
echo "$index = $person";
}
This is explained in the PHP foreach documentation.
Why would you need a numeric index inside associative array? Associative array maps arbitrary values to arbitrary values, like in your example, strings to strings and numbers:
$assoc = [
'name'=>'My name',
'creditcard'=>'234343435355',
'ssn'=>1450
];
Numeric arrays map consecutive numbers to arbitrary values. In your example if we remove all string keys, numbering will be like this:
$numb = [
0=>'My name',
1=>'234343435355',
2=>1450
];
In PHP you don't have to specify keys in this case, it generates them itself.
Now, to get keys in foreach statement, you use the following form, like #MichaelBerkowski already shown you:
foreach ($arr as $index => $value)
If you iterate over numbered array, $index will have number values. If you iterate over assoc array, it'll have values of keys you specified.
Seriously, why I am even describing all that?! It's common knowledge straight from the manual!
Now, if you have an associative array with some arbitrary keys and you must know numbered position of the values and you don't care about keys, you can iterate over result of array_values on your array:
foreach (array_values($assoc) as $value) // etc
But if you do care about keys, you have to use additional counter, like you shown yourself:
$counter = 0;
foreach ($assoc as $key => $value)
{
// do stuff with $key and $value
++$counter;
}
Or some screwed functional-style stuff with array_reduce, doesn't matter.

How to make sure the value is reset in a 'foreach' loop in PHP?

I was writing a simple PHP page and a few foreach loops were used.
Here are the scripts:
$arrs = array("a", "b", "c");
foreach ($arrs as $arr) {
if(substr($arr,0,1)=="b") {
echo "This is b";
}
} // End of first 'foreach' loop, and I didn't use 'ifelse' here.
And when this foreach ends, I wrote another foreach loop in which all the values in the foreach loop was the same as in the previous foreach.
foreach ($arrs as $arr) {
if(substr($arr,0,1)=="c") {
echo "This is c";
}
}
I am not sure if it is a good practice to have two foreach loops with the same values and keys.
Will the values get overwritten in the first foreach loop?
It's OK until you start using references and then you can get strange behaviour, for example:
<?php
$array = array(1,2,3,4);
//notice reference &
foreach ($array as & $item) { }
$array2 = array(10, 11, 12, 13);
foreach ($array2 as $item) { }
print_r($array);
Outputs this:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 13
)
Because the first foreach leaves $item as a reference to $array[3], $array[3] is sequentially set to each value in $array2.
You can solve this be doing unset($item) after the first foreach, which will remove the reference, but this isn't actually an issue in your case, as you are not using references with foreach.
Two notable notes from the documentation of foreach:
Note: When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop.
and
Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it.
Because you're not writing to the array, only reading from it and printing stuff out, no values in the array will be changed.
You can save time looping through the array twice though by doing this:
$arrs = array("a", "b", "c");
foreach ($arrs as $arr) {
if(substr($arr,0,1)=="b") {
echo "This is b";
}
if(substr($arr,0,1)=="c") {
echo "This is c";
}
}
All you need is 1 foreach loop.
Or an even shorter approach to what Jon said, is this:
$arrs = array("a", "b", "c");
foreach ($arrs as $arr)
if ($arr != "a")
echo 'This is ' . $arr;
This is much easier and faster than using substr, since you are already using a foreach loop. And try not to pollute it with a ton of if statements, if you find yourself doing this, it's better to use a switch statement, much faster.
Cheers :)

Categories