PHP json_encode() 函数介绍
在 php 中使用 json_encode() 内置函数(php > 5.2)可以使用得 php 中数据可以与其它语言很好的传递并且使用它。
这个函数的功能是将数值转换成json数据存储格式。
01<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none><?php
02<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit color:rgb background:none>$arr <codecourier new min-height:inherit background:none>= <codecourier new font-weight:bold min-height:inherit color:rgb background:none>array</codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>03<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none> <codecourier new min-height:inherit background:none>(</codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>04<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none> <codecourier new min-height:inherit color:blue background:none>'Name'<codecourier new min-height:inherit background:none>=><codecourier new min-height:inherit color:blue background:none>'你妹'<codecourier new min-height:inherit background:none>,</codecourier></codecourier></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>05<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none> <codecourier new min-height:inherit color:blue background:none>'Age'<codecourier new min-height:inherit background:none>=>22</codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>06<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none> <codecourier new min-height:inherit background:none>);</codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>07<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none> </tdcourier></codecourier></tdcourier></tr></tbody></table>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>08<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit color:rgb background:none>$jsonencode <codecourier new min-height:inherit background:none>=
json_encode(<codecourier new min-height:inherit color:rgb background:none>$arr<codecourier new min-height:inherit background:none>);</codecourier></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>09<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit color:rgb background:none>echo <codecourier new min-height:inherit color:rgb background:none>$jsonencode<codecourier new min-height:inherit background:none>;</codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>10<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>?></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>
<p>程序运行结果如下:</p>
<divcourier new min-height:inherit><p></p>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>1<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>{<codecourier new min-height:inherit color:blue background:none>"Name"<codecourier new min-height:inherit background:none>:null,<codecourier new min-height:inherit color:blue background:none>"Age"<codecourier new min-height:inherit background:none>:20}</codecourier></codecourier></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>
<p>json_encode 函数中中文被编码成 null 了,Google 了一下,很简单,为了与前端紧密结合,Json 只支持 utf-8 编码,我认为是前端的 Javascript 也是 utf-8 的原因。</p>
<divcourier new min-height:inherit><p></p>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>1<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none><?php
4 'name'=>iconv('GB18030','utf-8','你妹'),
8echo json_encode($array);
这个程序的运行结果为:
1{"title":"\u6d63\u72b2\ue785","body":"men"}
iconv 转换格式时用GBK,gb2312 报错问题,是因为GB2312只收录6763个汉字,出现未收录的汉字时,iconv报错。 GBK向下完全兼容GB2312-80,支持更多的字,但仍然会出现错误,解决办法是用GB18030,GB18030基本兼容GBK,又扩充了百余字体。数组中所有中文在json_encode之后都不见了或者出现\u6d63等。
解决方法是用urlencode()函数处理以下,在json_encode之前,把所有数组内所有内容都用urlencode()处理一下,然用json_encode()转换成json字符串,最后再用urldecode()将编码过的中文转回来。
01<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none><?php
02/**************************************************************
04 *
使用特定function对数组中所有元素做处理
05 *
@param string &$array 要处理的字符串
06 *
@param string $function 要执行的函数
07 *
@return boolean $apply_to_keys_also 是否也应用到key上
10 *************************************************************/
11function arrayRecursive(&$array, $function, $apply_to_keys_also =
false)
13 static $recursive_counter =
0;
14 if (++$recursive_counter >
1000) {
15 die('possible
deep recursion attack');
17 foreach ($array as $key => $value)
{
18 if (is_array($value))
{
19 arrayRecursive($array[$key], $function, $apply_to_keys_also);