This article describes PHP's Json Chinese processing solution. Share it with everyone for your reference, the details are as follows:
Json is a widely used format for transmitting strings. Compared with xml, it is simpler to understand and easier to operate. There are only two functions in PHP. , json_encode() AND json_deconde(). However, json's support for Chinese is not very good. If you use json_encode() to process an array, if there are Chinese characters in the array, it will be blanked.
One way to solve Chinese is to first convert Chinese to another encoding format, then use json_encode(), and finally use decoding to decode the json string. There is another way that is solved in the new version of PHP, as shown in the code below.
The following is a code example
<?php header("Content-type:text/html;charset=utf-8"); $arrayName = array('city' => '广东','goods'=>'cookies' ); $arr = json_encode($arrayName); echo $arr."</br>"; var_dump(json_decode($arr)); echo "</br>"; echo urldecode(json_encode(ch_json($arrayName)))."</br>"; /* 需要php版本在5.4以上 echo json_encode($arrayName,JSON_UNESCAPED_UNICODE); */ function ch_json($arr){ if(is_array($arr)){ foreach ($arr as $key => $value) { $arr[urlencode($key)] = ch_json($value); } }else{ return urlencode($arr); } return $arr; } ?>
I hope this article will be helpful to everyone in PHP programming.
For more articles related to PHP’s Json Chinese processing solutions, please pay attention to the PHP Chinese website!