PHP array to JSON display a code in Chinese, to explain
This post was last edited by wang23412 on 2013-06-09 17:05:27
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 > 1000) {
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. PHP JSON English Coding
Share to:
------Solution--------------------
Cough you can replace ' $function ' with the value of the parameter $function.
------Solution--------------------
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);