Recently in the interface code used to Json_encode, found on the internet said Json_encode encoding set to UTF-8 Chinese will not garbled, verified that this method is really effective, but do not know why, the code after a period of time is not very useful. Here's your solution to the json_encode. There is a better way please share it!
The first type:
This simple to do a code conversion, Urlcode and then return the desired array
This is enough for my code.
Code to copy code as follows
public static function Encodeoperations ($array)
{
foreach ((array) $array as $key = + $value) {
if (Is_array ($value)) {
Encodeoperations ($array [$key]);
} else {
$array [$key] = UrlEncode (mb_convert_encoding ($value, ' UTF-8 ', ' GBK '));
}
}
return $array;
}
The second type:
This is seen on the internet, and then there is a comment that there will be an infinite loop problem, but this is obviously very comprehensive, and then I do have after the test, posted here for reference only
Code to copy code as follows
/**************************************************************
*
* Processing of all elements in an array using a specific function
* @param string & $array strings to be processed
* After @param string $tocode encoded
* @param string $oldcode before encoding
* @param string $function the function to execute
* @return Boolean $apply _to_keys_also is also applied to the key
* @return The array $array is also applied to the key
* @access Public
*
*************************************************************/
Function Encodeoperations (& $array, $function, $tocode =false, $oldcode =false, $apply _to_keys_also = False)
{
foreach ($array as $key = = $value) {
(www.111cn.net)
if (Is_array ($value)) {
Encodeoperations ($array [$key], $function, $apply _to_keys_also);
} else {
if ($tocode && $oldcode) {
if (function_exists (mb_convert_encoding)) {
$value = mb_convert_encoding ($value, $tocode, $oldcode);
}else{
return "error";
}
}
$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]);
}
}
}
return $array;
}
/****from:http://www.111cn.net/phper/31/66729.htm**********************************************************
*
* Convert Array to JSON string (Chinese compatible)
* @param array $array to convert
* JSON string to be converted @return string
* @access Public
*
*************************************************************/
function JSON ($array) {
Arrayrecursive ($array, ' UrlEncode ', true);
$json = Json_encode ($array);
Return UrlDecode ($json);
}
If the return to Chinese is null, we need to convert the character to uft8 or use UrlEncode to compile the call first.
Json_encode UTF-8 Chinese garbled problem in PHP