You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$arrA = array('a' => array('x' => 100), 'b' => array('x' => 200), 'c' => array('x' => 300)); // Array of Arrays
$arrO = json_decode('[{"id": 1, "val": 10}, {"id": 2, "val": 20}, {"id": 3, "val": 30}]'); // Array of Objects
foreach ($arrA as &$val) $val['x'] += 2; // this doesnt work (variable $val)
//foreach ($arrA as &$val2) $val2['x'] += 2; // this works (renamed reference variable)
foreach ($arrO as $val) {
foreach ($arrA as $item) error_log($item['x']); // last item in $arrA changed from Array to Object in array $arrO !!!
break; // other itteration resulted in same error (last item changed)
}
Resulted in this output:
[05-Nov-2025 12:55:36 Europe/Bratislava] 102
[05-Nov-2025 12:55:36 Europe/Bratislava] 202
[05-Nov-2025 12:55:36 Europe/Bratislava] PHP Fatal error: Uncaught Error: Cannot use object of type stdClass as array in
But I expected this output instead:
[05-Nov-2025 12:54:32 Europe/Bratislava] 102
[05-Nov-2025 12:54:32 Europe/Bratislava] 202
[05-Nov-2025 12:54:32 Europe/Bratislava] 302
PHP Version
PHP 8.4.7 (cli) (built: May 6 2025 14:14:22) (NTS Visual C++ 2022 x64)
Copyright (c) The PHP Group
Zend Engine v4.4.7, Copyright (c) Zend Technologies
with Zend OPcache v8.4.7, Copyright (c), by Zend Technologies
with Xdebug v3.4.1, Copyright (c) 2002-2025, by Derick Rethans
Activity
iluuu1994 commentedon Nov 5, 2025
See #20327 and the plethora of similar issues. You need to
unset($var);to break the reference. https://3v4l.org/HiG2t