mirror of
https://github.com/php/php-src.git
synced 2025-08-15 21:48:51 +02:00

One strange feature of SplFixedArray was that it could not be used in nested foreach
loops. If one did so, the inner loop would overwrite the iteration state of the outer
loop.
To illustrate:
$spl = SplFixedArray::fromArray([0, 1]);
foreach ($spl as $a) {
foreach ($spl as $b) {
echo "$a $b";
}
}
Would only print two lines:
0 0
0 1
Use the new InternalIterator feature which was introduced in ff19ec2df3
to convert
SplFixedArray to an Aggregate rather than Iterable. As a bonus, we get to trim down
some ugly code! Yay!
19 lines
252 B
PHP
19 lines
252 B
PHP
--TEST--
|
|
Nested iteration of SplFixedArray using foreach loops
|
|
--FILE--
|
|
<?php
|
|
|
|
$array = SplFixedArray::fromArray([0, 1]);
|
|
|
|
foreach ($array as $value1) {
|
|
foreach ($array as $value2) {
|
|
echo "$value1 $value2\n";
|
|
}
|
|
}
|
|
|
|
?>
|
|
--EXPECT--
|
|
0 0
|
|
0 1
|
|
1 0
|
|
1 1
|