Online find said json_encode set to UTF-8 Chinese will not garbled, after a period of time is not useful, the following to share with you a better method recently used in the interface code json_encode, on the Internet find said json_encode encoding set to UTF-8 Chinese won't garbled, verified this method is indeed effective, but do not know why, the code won't work after a while. The following is your solution to json_encode. There are better ways to share it!
First:
This simple code conversion, urlcode and then return the required array
My code is enough.
The code is 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;}
Second:
This is seen on the Internet, and then there is a comment saying that there will be an infinite loop problem, but this is obviously very comprehensive, and I did have it after testing, posted here for reference only
The code is as follows:
/*************************************** * *********************** Use a specific function to process all elements in the array * @ param string & $ array: the string to be processed * @ param string $ After tocode encoding * @ param string $ Before oldcode encoding * @ param string $ function: the function to be executed * @ return boolean $ apply_to_keys_also? apply * @ return array $ array to key * @ access public ******************** **************************************** **/function encodeOperations (& $ array, $ Function, $ tocode = false, $ oldcode = false, $ apply_to_keys_also = false) {foreach ($ array as $ key => $ value) {(www.bitsCN.com) 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 ($ ap Ply_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.bitsCN.com/phper/31/66729.htm **************************************** * ******************* Convert an array to a JSON string (compatible with Chinese characters) * @ param array $ array the array to be converted * @ return string the converted json string * @ access public ***************** **************************************** * ***/function JSON ($ array) {arrayRecursive ($ array, 'urlencode', true); $ json = json_encode ($ array); return urldecode ($ json );}
If the return value for Chinese characters is null, convert the characters to uft8 or use urlencode to compile the call.