3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = '{"key1":"value1","key2":"value2"}'; $asObject = json_decode($json); var_dump($asObject); $asArray = json_decode($json, true); var_dump($asArray); foreach($asArray as $v){ echo $v; } // no problem; $asArray['anotherKey'] = 'anotherValue'; // no problem foreach($asObject as $v){ echo $v; } $asObject['anotherKey'] = 'anotherValue'; // E_ERROR : type 1 -- Cannot use object of type stdClass as array
Output for 7.0.0 - 7.0.31, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
object(stdClass)#1 (2) { ["key1"]=> string(6) "value1" ["key2"]=> string(6) "value2" } array(2) { ["key1"]=> string(6) "value1" ["key2"]=> string(6) "value2" } value1value2value1value2 Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /in/1dZLv:18 Stack trace: #0 {main} thrown in /in/1dZLv on line 18
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.38
object(stdClass)#1 (2) { ["key1"]=> string(6) "value1" ["key2"]=> string(6) "value2" } array(2) { ["key1"]=> string(6) "value1" ["key2"]=> string(6) "value2" } value1value2value1value2 Fatal error: Cannot use object of type stdClass as array in /in/1dZLv on line 18
Process exited with code 255.

preferences:
107.08 ms | 2062 KiB | 4 Q