3v4l.org

run code in 300+ PHP versions simultaneously
<?php if (!function_exists('json_encode')) { function json_encode($it) { if (!($assoc = is_object($it)) && !is_array($it)) { return _js_enc_value($it); } $i = 0; $it = (array)$it; if (!$assoc) { foreach ($it as $k => $v) { if ($k !== $i) { $assoc = true; break; } $i++; } } var_dump($assoc); $data = array(); foreach ($it as $k => $v) { $v = _js_enc_value($v); if ($assoc) { $k = _js_enc_value((string)$k); $data[] = $k . ':' . $v; } else { $data[] = $v; } } if ($assoc) { $bl = '{'; $br = '}'; } else { $bl = '['; $br = ']'; } return $bl . implode(',', $data) . $br; } function _js_enc_value($value) { var_dump($value); switch (true) { case is_array($value): case is_object($value): return json_encode($value); case $value === null: case is_resource($value): return 'null'; case is_bool($value): return $value ? 'true' : 'false'; case is_string($value): return '"' . _js_uc_esc($value) . '"'; default: return $value; } } function _js_uc_esc($value) { $pattern = "@[\x08-\x10\x0c\x0d\\\\\"/]|([^\x08-\x10\x0c\x0d\x32-\x7f\\\\\"/])++@u"; return preg_replace_callback($pattern, '_js_uc_esc_cb', $value); } function _js_uc_esc_cb($matches) { switch (true) { case isset($matches[1]): $char = mb_convert_encoding($matches[0], 'UTF-16', 'UTF-8'); $str = ''; $len = strlen($char); for ($i = 0; $i < $len; $i += 2) { $str .= sprintf('\\u%02x%02x', ord($char[$i]), ord($char[$i + 1]) ); } return $str; default: return addcslashes($matches[0], "\x08\x09\x10\x0c\x0d\\\"/"); } } } $var = array( array( 'text', 1, 1.1, true, false, null, array(), new stdClass, ), array( '&\'/"' => "\r\n\t", ), array( '0' => 'hoge', '1' => 'foo', ), (object)array( '0' => 'hoge', '1' => 'foo', ), array( '0' => 'hoge', '2' => 'foo', ), (object)array( '0' => 'hoge', '2' => 'foo', ), ); var_dump(@json_encode($var));
Output for 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 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.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
string(142) "[["text",1,1.1,true,false,null,[],{}],{"&'\/\"":"\r\n\t"},["hoge","foo"],{"0":"hoge","1":"foo"},{"0":"hoge","2":"foo"},{"0":"hoge","2":"foo"}]"
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6
bool(false) array(8) { [0]=> string(4) "text" [1]=> int(1) [2]=> float(1.1) [3]=> bool(true) [4]=> bool(false) [5]=> NULL [6]=> array(0) { } [7]=> object(stdClass)#1 (0) { } } bool(false) string(4) "text" int(1) float(1.1) bool(true) bool(false) NULL array(0) { } bool(false) object(stdClass)#1 (0) { } bool(true) array(1) { ["&'/""]=> string(3) " " } bool(true) string(3) " " string(4) "&'/"" array(2) { [0]=> string(4) "hoge" [1]=> string(3) "foo" } bool(false) string(4) "hoge" string(3) "foo" object(stdClass)#2 (2) { [0]=> string(4) "hoge" [1]=> string(3) "foo" } bool(true) string(4) "hoge" string(1) "0" string(3) "foo" string(1) "1" array(2) { [0]=> string(4) "hoge" [2]=> string(3) "foo" } bool(true) string(4) "hoge" string(1) "0" string(3) "foo" string(1) "2" object(stdClass)#3 (2) { [0]=> string(4) "hoge" [2]=> string(3) "foo" } bool(true) string(4) "hoge" string(1) "0" string(3) "foo" string(1) "2" string(171) "[["text",1,1.1,true,false,null,[],{}],{"\u0026\u0027\/\"":"\r \t"},["hoge","foo"],{"\u0030":"hoge","\u0031":"foo"},{"\u0030":"hoge","2":"foo"},{"\u0030":"hoge","2":"foo"}]"
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9
bool(false) array(8) { [0]=> string(4) "text" [1]=> int(1) [2]=> float(1.1) [3]=> bool(true) [4]=> bool(false) [5]=> NULL [6]=> array(0) { } [7]=> object(stdClass)(0) { } } bool(false) string(4) "text" int(1) float(1.1) bool(true) bool(false) NULL array(0) { } bool(false) object(stdClass)(0) { } bool(true) array(1) { ["&'/""]=> string(3) " " } bool(true) string(3) " " string(4) "&'/"" array(2) { [0]=> string(4) "hoge" [1]=> string(3) "foo" } bool(false) string(4) "hoge" string(3) "foo" object(stdClass)(2) { [0]=> string(4) "hoge" [1]=> string(3) "foo" } bool(true) string(4) "hoge" string(1) "0" string(3) "foo" string(1) "1" array(2) { [0]=> string(4) "hoge" [2]=> string(3) "foo" } bool(true) string(4) "hoge" string(1) "0" string(3) "foo" string(1) "2" object(stdClass)(2) { [0]=> string(4) "hoge" [2]=> string(3) "foo" } bool(true) string(4) "hoge" string(1) "0" string(3) "foo" string(1) "2" string(171) "[["text",1,1.1,true,false,null,[],{}],{"\u0026\u0027\/\"":"\r \t"},["hoge","foo"],{"\u0030":"hoge","\u0031":"foo"},{"\u0030":"hoge","2":"foo"},{"\u0030":"hoge","2":"foo"}]"

preferences:
265.15 ms | 404 KiB | 381 Q