Because the json_encode function must be supported by php5.20 or later, how can I use the json function if php does not support json_encode? There are no alternative functions, but there is no alternative function. However, some people have made a custom php_json_encode. it is unknown what problems will occur during use.
Because the json_encode function must be supported by php5.20 or later, how can I use the json function if php does not support json_encode? There are no alternative functions, but there is no alternative function. However, some people have made a custom php_json_encode. it is unknown what problems will occur during use.
Http://www.php.net/manual/zh/function.json-encode.php#74878
[Php]
Function php_json_encode ($ arr)
{
$ Json_str = "";
If (is_array ($ arr ))
{
$ Pure_array = true;
$ Array_length = count ($ arr );
For ($ I = 0; $ I <$ array_length; $ I ++)
{
If (! Isset ($ arr [$ I])
{
$ Pure_array = false;
Break;
}
}
If ($ pure_array)
{
$ Json_str = "[";
$ Temp = array ();
For ($ I = 0; $ I <$ array_length; $ I ++)
{
$ Temp [] = sprintf ("% s", php_json_encode ($ arr [$ I]);
}
$ Json_str. = implode (",", $ temp );
$ Json_str. = "]";
}
Else
{
$ Json_str = "{";
$ Temp = array ();
Foreach ($ arr as $ key => $ value)
{
$ Temp [] = sprintf ("" % s ": % s", $ key, php_json_encode ($ value ));
}
$ Json_str. = implode (",", $ temp );
$ Json_str. = "}";
}
}
Else
{
If (is_string ($ arr ))
{
$ Json_str = ". json_encode_string ($ arr ).""";
}
Else if (is_numeric ($ arr ))
{
$ Json_str = $ arr;
}
Else
{
$ Json_str = ". json_encode_string ($ arr ).""";
}
}
Return $ json_str;
}
Function json_encode_string ($ in_str ){
Mb_internal_encoding ("UTF-8 ");
$ Convmap = array (0x80, 0 xFFFF, 0, 0 xFFFF );
$ Str = "";
For ($ I = mb_strlen ($ in_str)-1; $ I >=0; $ I --){
$ Mb_char = mb_substr ($ in_str, $ I, 1 );
If (mb_ereg ("& # (\ d +);", mb_encode_numericentity ($ mb_char, $ convmap, "UTF-8"), $ match )){
$ Str = sprintf ("\ u % 04x", $ match [1]). $ str;
} Else {
$ Str = $ mb_char. $ str;
}
}
Return $ str;
}
[/Php]
Json_encode substitution function php_json_encode