Skip to content

bug in all 8.x versions #20396

Closed as duplicate
Closed as duplicate

Description

@Slavo-S

Description

The following code:

    $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

Operating System

Windows10

Activity

  1. iluuu1994 commented on Nov 5, 2025

    @iluuu1994
    Member

    See #20327 and the plethora of similar issues. You need to unset($var); to break the reference. https://3v4l.org/HiG2t

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions