3v4l.org

run code in 300+ PHP versions simultaneously
<?php $r = array('hoge'=> new \stdClass()); $j = json_serialize($r); echo $j . "\n"; print_r(json_unserialize($j)); echo "\n"; function json_serialize($any) { return json_encode(json_wrap($any)); } function json_unserialize($str) { return json_unwrap(json_decode($str)); } function json_wrap($any, $skipAssoc = false) { if (!$skipAssoc && is_array($any) && is_string(key($any))) { return (object)array("_PHP_ASSOC" => json_wrap($any,true)); } if (is_array($any) || is_object($any)) { foreach ($any as &$v) { $v = json_wrap($v); } } return $any; } function json_unwrap($any, $skipAssoc = false) { if (!$skipAssoc && is_object($any) && is_object($any->_PHP_ASSOC) && count((array)$any) == 1) { return (array)json_unwrap($any->_PHP_ASSOC); } if (is_array($any) || is_object($any)) { foreach ($any as &$v) { $v = json_unwrap($v); } } return $any; }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
{"_PHP_ASSOC":{"hoge":{}}} Warning: Undefined property: stdClass::$_PHP_ASSOC in /in/vZctd on line 29 Warning: Undefined property: stdClass::$_PHP_ASSOC in /in/vZctd on line 29 Array ( [hoge] => stdClass Object ( ) )
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.37, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
{"_PHP_ASSOC":{"hoge":{}}} Notice: Undefined property: stdClass::$_PHP_ASSOC in /in/vZctd on line 29 Notice: Undefined property: stdClass::$_PHP_ASSOC in /in/vZctd on line 29 Array ( [hoge] => stdClass Object ( ) )
Output for 7.3.32 - 7.3.33
{"_PHP_ASSOC":{"hoge":{}}} Array ( [hoge] => stdClass Object ( ) )
Output for 5.2.6 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/vZctd on line 2 {"_PHP_ASSOC":{"hoge":{}}} Notice: Undefined property: stdClass::$_PHP_ASSOC in /in/vZctd on line 29 Notice: Undefined property: stdClass::$_PHP_ASSOC in /in/vZctd on line 29 Array ( [hoge] => stdClass Object ( ) )
Output for 5.2.0 - 5.2.5
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/vZctd on line 2 {"_PHP_ASSOC":{"hoge":{}}} Notice: Undefined property: stdClass::$_PHP_ASSOC in /in/vZctd on line 29 Notice: Undefined property: stdClass::$_PHP_ASSOC in /in/vZctd on line 29 Array ( [hoge] => stdClass Object ( ) )
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/vZctd on line 2 Fatal error: Call to undefined function json_encode() in /in/vZctd on line 9
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/vZctd on line 2 Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' in /in/vZctd on line 21
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/vZctd on line 2 Parse error: parse error, unexpected '&', expecting T_VARIABLE or '$' in /in/vZctd on line 21
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/vZctd on line 2 Parse error: parse error, expecting `T_VARIABLE' or `'$'' in /in/vZctd on line 21
Process exited with code 255.

preferences:
215.59 ms | 402 KiB | 365 Q