这篇文章主要介绍了PHP针对JSON操作的常用方法,实例分析了json转数组、数组转json等技巧与相关注意事项,需要的朋友可以参考下
本文实例分析了PHP针对JSON操作。分享给大家供大家参考。具体分析如下:
由于JSON可以在很多种程序语言中使用,所以我们可以用来做小型数据中转,如:PHP输出JSON字符串供JavaScript使用等。在PHP中可以使用 json_decode() 由一串规范的字符串解析出 JSON对象,使用 json_encode() 由JSON 对象生成一串规范的字符串。
例:
代码如下:
<?php
$json = ‘{“a”:1, “b”:2, “c”:3, “d”:4, “e”:5 }’;
var_dump(json_decode($json));
var_dump(json_decode($json,true));
$json = ‘{“a”:1, “b”:2, “c”:3, “d”:4, “e”:5 }’;
var_dump(json_decode($json));
var_dump(json_decode($json,true));
输出:
代码如下:
object(stdClass)#1 (5) {
[“a”] => int(1)
[“b”] => int(2)
[“c”] => int(3)
[“d”] => int(4)
[“e”] => int(5)
}
[“a”] => int(1)
[“b”] => int(2)
[“c”] => int(3)
[“d”] => int(4)
[“e”] => int(5)
}
array(5) {
[“a”] => int(1)
[“b”] => int(2)
[“c”] => 来源gao@dai!ma.com搞$代^码网int(3)
[“d”] => int(4)
[“e”] => int(5)
}
以上就是PHP针对JSON操作实例分析的详细内容,更多请关注gaodaima搞代码网其它相关文章!