3v4l.org

run code in 300+ PHP versions simultaneously
<?php function JSEscape($s) { static $aSearch = array("\xe2\x80\xa9", "\\", "'", "\"", "\r\n", "\r", "\n", "\xe2\x80\xa8", "*/", "</"); static $aReplace = array(" ", "\\\\", "\\'", '\\"', "\n", "\n", "\\n", "\\n", "*\\/", "<\\/"); $val = str_replace($aSearch, $aReplace, $s); return $val; } function PhpToJSObject($arData, $bWS = false, $bSkipTilda = false, $bExtType = false) { static $aSearch = array("\r", "\n"); $bExtType = !!$bExtType; if(is_array($arData)) { $i = -1; $j = -1; if (!empty($arData)) { foreach($arData as $j => $temp) { $i++; if ($j !== $i) break; } } if($j === $i) { foreach($arData as $key => $value) { if(is_string($value)) { if(preg_match("#['\"\\n\\r<\\\\\x80]#", $value)) $arData[$key] = "'".JSEscape($value)."'"; else $arData[$key] = "'".$value."'"; } elseif(is_bool($value)) { if($value === true) $arData[$key] = 'true'; else $arData[$key] = 'false'; } elseif(is_array($value)) { $arData[$key] = PhpToJSObject($value, $bWS, $bSkipTilda, $bExtType); } elseif ($bExtType && (is_int($value) || is_float($value))) { $arData[$key] = $value; } else { if(preg_match("#['\"\\n\\r<\\\\\x80]#", $value)) $arData[$key] = "'".JSEscape($value)."'"; else $arData[$key] = "'".$value."'"; } } return '['.implode(',', $arData).']'; } $sWS = ','.($bWS ? "\n" : ''); $res = ($bWS ? "\n" : '').'{'; $first = true; foreach($arData as $key => $value) { if ($bSkipTilda && substr($key, 0, 1) == '~') continue; if($first) $first = false; else $res .= $sWS; if(preg_match("#['\"\\n\\r<\\\\\x80]#", $key)) $res .= "'".str_replace($aSearch, '', JSEscape($key))."':"; else $res .= "'".$key."':"; if(is_string($value)) { if(preg_match("#['\"\\n\\r<\\\\\x80]#", $value)) $res .= "'".JSEscape($value)."'"; else $res .= "'".$value."'"; } elseif(is_bool($value)) { if($value === true) $res .= 'true'; else $res .= 'false'; } elseif(is_array($value)) { $res .= PhpToJSObject($value, $bWS, $bSkipTilda, $bExtType); } elseif ($bExtType && (is_int($value) || is_float($value))) { $res .= $value; } else { if(preg_match("#['\"\\n\\r<\\\\\x80]#", $value)) $res .= "'".JSEscape($value)."'"; else $res .= "'".$value."'"; } } $res .= ($bWS ? "\n" : '').'}'; return $res; } elseif(is_bool($arData)) { if($arData === true) return 'true'; else return 'false'; } elseif ($bExtType && (is_int($arData) || is_float($arData))) { return $arData; } else { if(preg_match("#['\"\\n\\r<\\\\\x80]#", $arData)) return "'".JSEscape($arData)."'"; else return "'".$arData."'"; } } $a=array( "a0" => "b", "a1" => "b", "a2" => "b", "a3" => "b", "b" => "c'", "c" => array(1,2,3), "d" => true, "e" => 15.5 ); print_r(PhpToJSObject($a));
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.7, 7.2.29 - 7.2.33, 7.3.16 - 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.7
{'a0':'b','a1':'b','a2':'b','a3':'b','b':'c\'','c':['1','2','3'],'d':true,'e':'15.5'}

preferences:
196.34 ms | 406 KiB | 310 Q