<? = array ( 'Name'= ' Shia ', ' Age ' = = Json_encode ($arr); echo $jsonencode;? >
The results of the program run as follows:
{"Name":null,"Age":+}
Json_encode function in Chinese is encoded as NULL, Google a bit, very simple, in order to closely integrate with the front, Json only support utf-8 encoding, I think is the front-end Javascript is also the reason for Utf-8.
<? php$array = Array ( ' => ' abcd ... ?
The result of this program is:
{"title":"\u8fd9\u91cc\u662f\u4e2d\u6587\u6807\u9898", " Body ":"abcd ... "}
All the Chinese in the array have disappeared after the Json_encode, or appear \u2353.
The workaround is to use the UrlEncode () function to handle the following, before Json_encode, all the contents of all arrays are processed with UrlEncode (), and then converted to a JSON string with Json_encode (), and then UrlDecode () Turn the encoded Chinese back.
<?PHP/************************************************************** * * Handle all elements in an array with a specific function * @param string & $array The string to be processed * @param string $function the function to execute * @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 > +) {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--;} /************************************************************** * * Converts an array to a JSON string (Chinese-compatible) * @param array $arra Y the array to convert * @return the JSON string converted from String * @access public * ******************************************** *****************/function JSON ($array) {arrayrecursive ($array,'UrlEncode',true); $json=Json_encode ($array); returnUrlDecode ($json);} $array=Array ('Name'='Shia', ' Age'= -); Echo JSON ($array);?>
This success, the results of the operation are as follows:
{"Name":" Shia ","Age":" - "}
Original link: http://www.nowamagic.net/php/php_FunctionJsonEncode.php
PHP Jsonencode already contains the processing of Chinese characters in JSON