Home > php教程 > PHP开发 > body text

Teach you how to use PHP to output Chinese JSON strings

高洛峰
Release: 2016-12-27 16:20:58
Original
1096 people have browsed it

json_endoce: http://cn.php.net/json_encode
json_dedoce: http://cn.php.net/json_decode
Copy after login

json_encode — JSON encodes the variable and returns the JSON form of the value, for example:

<?php
$arr = array (&#39;a&#39;=>1,&#39;b&#39;=>2,&#39;c&#39;=>3,&#39;d&#39;=>4,&#39;e&#39;=>5);
echo json_encode($arr);
?>
Copy after login

The output after the above code is executed:

{"a":1,"b":2,"c":3,"d":4,"e":5}
Copy after login

If the data source to be encoded (generally is an array), the value contains Chinese, and the output after json_encode processing is unicode encoding.

<?php
$arr = array (&#39;a&#39;=>&#39;脚本之家&#39;);
echo json_encode($arr);
?>
Copy after login

The output after the above code is executed:

{"a":"\u811a\u672c\u4e4b\u5bb6"}
Copy after login

The bottom layer of PHP has been processed by unicode. If you think it is not intuitive enough, you can use the urlencode and urldecode methods to bypass the process of transcoding to unicode:

$arr = array (&#39;a&#39;=>urlencode(&#39;PHP中文网&#39;));
echo urldecode(json_encode($arr));
Copy after login

After the above code is executed, the output is:

{"a":"PHP中文网"}
Copy after login

For more related articles that teach you how to use PHP to output Chinese JSON strings, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!