The json_encode () built-in function (phpgt; 5.2) in php can be used to transmit and use data in php with other languages, this function converts a value to a json data storage format .... the json_encode () built-in function (php> 5.2) in php can be used to transmit and use data in php with other languages, this function converts a value to a json data storage format.
1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5); echo json_encode ($ arr ); // result // {"a": 1, "B": 2, "c": 3, "d": 4, "e": 5}?>
The following describes a json_encode Chinese garbled problem:
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.
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_e Ncode ($ array); return urldecode ($ json);} $ array = array ('name' => 'xia', 'age' => 20 ); echo json ($ array);?>
Application instance: