Json parsing: I have solved the problem that json_encode cannot pass Chinese characters. in the case of gbk encoding, how can I use the json_decode () function to parse it? PHPcodefunctionarrayRecursive (& amp; $ array, $ function, $ apply_to_keys_alsofalse) {static $ r json parsing
Q: I have solved the problem that json_encode cannot pass Chinese characters in gbk encoding. but how can I use the json_decode () function to parse it?
PHP code
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_k Ey = $ function ($ key); if ($ new_key! = $ Key) {$ array [$ new_key] = $ array [$ key]; unset ($ array [$ key]) ;}}$ recursive_counter --;} /*************************************** * ************************ 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); $ json = urldecode ($ json ); $ json = str_replace ("\" false \ "", "false", $ json); $ json = str_replace ("\" true \ "", "true ", $ json); return $ json;} function get_goodbook () {$ SQL = "select * from 'Wiki _ doc "'; $ data = $ this-> db-> getAll ($ SQL); return JSON ($ data );}
------ Solution --------------------
Json_encode can be used to transmit Chinese characters. iconv is used to input logs.
------ Solution --------------------
You encoded the content as a url. how do you know you need to decode the content after the other party (such as js) obtains it?
You should perform iconv ('gbk', 'utf-8', $ value) encoding conversion in the arrayRecursive function.
------ Solution --------------------
You can encapsulate it yourself.
PHP code
$ Ar = array ('Here is GBK encoded data', array ('This is a test', 'this is a test ',),); echo $ s = json_encode (array_map ('gb2utf', $ ar); print_r (array_map ('utf2gb', json_decode ($ s); function gb2utf ($ v) {if (! Is_array ($ v) return iconv ('gbk', 'utf-8', $ v); foreach ($ v as & $ t) $ t = gb2utf ($ t); return $ v;} function utf2gb ($ v) {if (! Is_array ($ v) return iconv ('utf-8', 'gbk', $ v); foreach ($ v as & $ t) $ t = utf2gb ($ t); return $ v ;}
------ Solution --------------------
First, convert the array to UTF-8 to encode Chinese characters. there is no need to write many other codes.