首页  >  文章  >  后端开发  >  详解php json相关函数的用法示例

详解php json相关函数的用法示例

黄舟
黄舟原创
2017-03-29 09:04:171309浏览

这篇文章主要介绍了php json相关函数用法,列举了json_encode、json_decode及json_last_error的功能,并实例分析了json_encode与json_decode函数的具体使用技巧,需要的朋友可以参考下

本文实例讲述了php json相关函数用法。分享给大家供大家参考,具体如下:

函数列表:

函数 描述
json_encode 变量进行json编码
json_decode 对json格式的字符串进行解码,转换为php变量
json_last_error 返回最后发生的错误


For example 1:
json_encode

$arr=array("A"=>"a","B"=>"b","C"=>"c","D"=>"d");
echo json_encode($arr);

output:

{"A":"a","B":"b","C":"c","D":"d"}

For example 2: json_decode

$arr='{"A":"a","B":"b","C":"c","D":"d"}';
var_dump(json_decode($arr));
var_dump(json_decode($arr,true));

output:

object(stdClass)[1]
 public 'A' => string 'a' (length=1)
 public 'B' => string 'b' (length=1)
 public 'C' => string 'c' (length=1)
 public 'D' => string 'd' (length=1)
array (size=4)
 'A' => string 'a' (length=1)
 'B' => string 'b' (length=1)
 'C' => string 'c' (length=1)
 'D' => string 'd' (length=1)

以上是详解php json相关函数的用法示例的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn