今天开发的时遇见了一个平台不怎么注意的问题 就是再对json_decode解析完josn数组的时候变成了 object 对象
<?php
$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)
}
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}可以看出 json_decode($data,true)输出的一个关联数组,由此可知json_decode($data)输出的是对象,而json_decode("$arr",true)是把它强制生成PHP关联数组.
本文探讨了PHP中使用json_decode函数处理JSON数据时的区别,重点介绍了如何通过设置第二个参数为true来将JSON数据转换为PHP关联数组而非对象。
424

被折叠的 条评论
为什么被折叠?



