Use the json_encode () built-in function (php & gt; 5.2) in php to transfer and use data in php with other languages. This function converts a value to a json data storage format. $ Arrarray (& #39; Name & #39; & gt; & #39; Wukong & #39;, & #39; Age & #39; & gt; 5000 using the json_encode () built-in function in php (php> 5.2) can use php to transfer data well with other languages and use it.
This function converts a value to a json data storage format.
$ Arr = array ('name' => 'wukong', 'age' => 5000); $ jsonencode = json_encode ($ arr); echo $ jsonencode;
The program running result is as follows:
{"Name":null,"Age":5000}
In the json_encode function, Chinese characters are encoded as null. after Google, it is very simple. to work closely with the front-end, Json only supports UTF-8 encoding. I think the front-end Javascript is also the reason for UTF-8 encoding.
$ Array = array ('title' => iconv ('gb2312', 'utf-8', 'Here is the Chinese title'), 'body' => 'abcd... '); echojson_encode ($ array );
The running result of this program is:
{"title":"\u8fd9\u91cc\u662f\u4e2d\u6587\u6807\u9898","body":"abcd..."}
All Chinese characters in the array are lost after json_encode or \ u2353 is displayed.
The solution is to use the urlencode () function to process the following. before json_encode, urlencode () is used to process all the content in the array, and json_encode () is used to convert the content into a json string, finally, use urldecode () to convert the encoded Chinese characters back.
/*************************************** * *********************** Use a specific function to process all elements in the array * @ param string & $ array the string to be processed * @ param string $ function the function to be executed * @ return boolean $ apply_to_keys_also is also applied to the key * @ access public ******** **************************************** * ************/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 --;} /*************************************** * ************************ 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 );} $ array = array ('name' => 'wukong, 'age' => 5000); echo JSON ($ array );
The operation is successful. The result is as follows:
{"Name": "Wukong", "Age": "5000 "}