Consider the following code:
$a = [1, 2, 3];
foreach ($a as $x) {
foreach ($a as $y) {
echo "$x $y\n";
}
}
As expected, it gives the following result:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Although I am pleased with the result, I am surprised that it works because, according to manual , it relies on an internal array pointer
When starting foreach for the first time, 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 the foreach loop.
Because foreach relies on a pointer to an internal array, changing it in a loop can lead to unexpected behavior.
Thus, we could expect that using the nested foreachinside the first one will have the same internal pointer and give the following result:
1 1
1 2
1 3
foreach , foreach.
:
- PHP /, PHP ,
foreach; foreach , .
?