[php-src] Issue #11885: foreach ($arr as $item) corrupts last item of an array
| From: | jwwq | Date: | Sat, 05 Aug 2023 18:22:47 +0000 |
| Subject: | [php-src] Issue #11885: foreach ($arr as $item) corrupts last item of an array | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245109@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11885
Author: jwwq
### Description
TLDR: Calling "foreach" after "foreach ($arr as &$item)" for
multidimensional array corrupts last item of an array (see comments in the code)
(
The following code:
```php
<?php
echo '<pre>';
$arr =
[
[1,'a'],
[3,'b'],
[5,'c'],
[7,'d'],
];
foreach ($arr as &$item)
$item[0] = $item[0]+100;
// anything ok at this point
print_r($arr);
// seems innocent...
foreach ($arr as $item) {}
// item[3] was corrupted
print_r($arr);
?>
```
Resulted in this output:
```
Array
(
[0] => Array
(
[0] => 101
[1] => a
)
[1] => Array
(
[0] => 103
[1] => b
)
[2] => Array
(
[0] => 105
[1] => c
)
[3] => Array
(
[0] => 107
[1] => d
)
)
Array
(
[0] => Array
(
[0] => 101
[1] => a
)
[1] => Array
(
[0] => 103
[1] => b
)
[2] => Array
(
[0] => 105
[1] => c
)
[3] => Array
(
[0] => 105
[1] => c
)
)
```
But I expected this output instead:
Both print_r's should be the same.
### PHP Version
PHP 8.2.9
### Operating System
Windows 11