Home >Backend Development >PHP Tutorial >How to process Json Chinese in php
This article mainly introduces PHP's Json Chinese processing solution, involving related operating skills of PHP encoding conversion, and has certain reference value. Friends in need can refer to it
This article tells about PHP's Json Chinese processing solutions. Share it with everyone for your reference, the details are as follows:
Json is now a widely used format for transmitting strings. Compared with xml, it is simpler to understand and more convenient to operate. There are only two functions under php, json_encode() AND json_deconde(). However, json's support for Chinese is not very good. If you use json_encode() to process such as 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; } ?>
The above is the detailed content of How to process Json Chinese in php. For more information, please follow other related articles on the PHP Chinese website!