PHP Json_encode UTF-8 A better way to solve the Chinese garbled,
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)) {Enco Deoperations ($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
The code is as follows
/**************************************************************** uses a specific function to manipulate all elements in an array * @param string &$ The string to be processed by array * @param string $tocode encoded * @param string $oldcode before encoding * @param string $function the function to execute * @return Boolean $app Whether the ly_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.jb51.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.bkjia.com/phper/31/66729.htm************************************************************ Converts an array to a JSON string (Chinese-compatible) * @param array $array arrays to be converted * @return string-converted JSON 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.
http://www.bkjia.com/PHPjc/886146.html www.bkjia.com true http://www.bkjia.com/PHPjc/886146.html techarticle php json_encode UTF-8 Chinese garbled better solution, 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, ...