3v4l.org

run code in 300+ PHP versions simultaneously
<?php function array_to_anon_object($arr): object { $obj = new class {}; foreach ( $arr as $key => $value ) { $obj->{$key} = $value; } return $obj; } class EmptyClass {} function array_to_normal_object($arr, $className): object { $obj = new class {}; foreach ( $arr as $key => $value ) { $obj->{$key} = $value; } return $obj; } var_dump( json_encode([1,2,3]) ); var_dump( json_encode((object)[1,2,3]) ); var_dump( json_encode(array_to_anon_object([1,2,3])) ); var_dump( json_encode(array_to_normal_object([1,2,3], EmptyClass::class)) ); var_dump( json_encode(array_to_normal_object([1,2,3], stdClass::class)) );
Output for 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
string(7) "[1,2,3]" string(19) "{"0":1,"1":2,"2":3}" Deprecated: Creation of dynamic property class@anonymous::$0 is deprecated in /in/fGRBj on line 6 Deprecated: Creation of dynamic property class@anonymous::$1 is deprecated in /in/fGRBj on line 6 Deprecated: Creation of dynamic property class@anonymous::$2 is deprecated in /in/fGRBj on line 6 string(19) "{"0":1,"1":2,"2":3}" Deprecated: Creation of dynamic property class@anonymous::$0 is deprecated in /in/fGRBj on line 16 Deprecated: Creation of dynamic property class@anonymous::$1 is deprecated in /in/fGRBj on line 16 Deprecated: Creation of dynamic property class@anonymous::$2 is deprecated in /in/fGRBj on line 16 string(19) "{"0":1,"1":2,"2":3}" Deprecated: Creation of dynamic property class@anonymous::$0 is deprecated in /in/fGRBj on line 16 Deprecated: Creation of dynamic property class@anonymous::$1 is deprecated in /in/fGRBj on line 16 Deprecated: Creation of dynamic property class@anonymous::$2 is deprecated in /in/fGRBj on line 16 string(19) "{"0":1,"1":2,"2":3}"
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33
string(7) "[1,2,3]" string(19) "{"0":1,"1":2,"2":3}" string(19) "{"0":1,"1":2,"2":3}" string(19) "{"0":1,"1":2,"2":3}" string(19) "{"0":1,"1":2,"2":3}"

preferences:
139.64 ms | 409 KiB | 5 Q