This post was last edited by wang23412 on 2013-06-09 17:05:27
PHP JSON Chinese encoding
When I passed the array into JSON format in PHP, I found that Chinese would become a strange encoding. So on the Internet to find a solution, see a lot of people with the following two functions solved:
Function arrayrecursive (& $array, $function, $apply _to_keys_also = False) { static $recursive _counter = 0; if (+ + $recursive _counter >) {die (' possible deep recursion attack '); } foreach ($array as $key = + $value) { if (Is_array ($value)) { arrayrecursive ($array [$key], $function, $apply _ to_keys_also); } else { $array [$key] = $function ($value); } if ($apply _to_keys_also && is_string ($key)) { $new _key = $function ($key); if ($new _key! = $key) { $array [$new _key] = $array [$key]; Unset ($array [$key]); }} $recursive _counter--;}
function JSON ($array) {arrayrecursive ($array, ' UrlEncode ', true), $json = Json_encode ($array); return UrlDecode ($json);}
I tested the next really can solve the problem, so carefully studied the next code, found the following code does not know what it means:
$array [$key] = $function ($value);
I output $array[$key] before and after this sentence, and I found this sentence to turn Chinese into a string of codes.
Please explain what $function ($value) means.
Reply to discussion (solution)
Cough you can replace ' $function ' with the value of the parameter $function.
Defined
Function arrayrecursive (& $array, $function, $apply _to_keys_also = False)
$array [$key] = $function ($value);
Call
Arrayrecursive ($array, ' UrlEncode ', true);
So $array [$key] = $function ($value);
is $array [$key] = UrlEncode ($value);
Uh.. All right.. Is the parameter passed in. I'm low-end ...
Defined
Function arrayrecursive (& $array, $function, $apply _to_keys_also = False)
$array [$key] = $function ($value);
Call
Arrayrecursive ($array, ' UrlEncode ', true);
So $array [$key] = $function ($value);
is $array [$key] = UrlEncode ($value);
So it seems that the function of these two functions is to first urlencode each value in the array, then json_encode into JSON format, and finally the JSON-formatted string urldecode. Thank you. Look at the code, you can't even turn your head.
This function is for GBK encoded data.
For utf-8 data, this should not be necessary.