[php-src] Issue #11621: Unexpected Array Modification During Foreach Loop with References
| From: | Fell-x27 | Date: | Fri, 07 Jul 2023 16:12:44 +0000 |
| Subject: | [php-src] Issue #11621: Unexpected Array Modification During Foreach Loop with References | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-244905@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11621
Author: Fell-x27
### Description
The following code:
```php
<?php
$arr = [1,2,3,4,5];
$pewpew = &$arr[2];
var_export($arr); //[1,2,3,4,5]
foreach($arr as $key => $pewpew){
//do nothing
}
var_export($arr); //[1,2,5,4,5] 5 instead on 3 at [2] position!
```
Resulted in this output:
```
array (
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
)
array (
0 => 1,
1 => 2, //see?
2 => 5,
3 => 4,
4 => 5,
)
```
But I expected this output instead:
```
array (
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
)
array (
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
)
```
Demo with normal and abnormal behaviour: https://3v4l.org/f0CMA
### PHP Version
8.2.8
### Operating System
Ubuntu 20.04