发布日期:2018-03-26
在PHP中使用json_encode / json_decode将stdClass转化为数组+ 查看更多
在PHP中使用json_encode / json_decode将stdClass转化为数组
+ 查看更多
发布日期:2018-03-10 16:39
分类:PHP
浏览次数:261
请看这个小脚本:
$array = array('stuff' => 'things'); print_r($array); //prints - Array ( [stuff] => things ) $arrayEncoded = json_encode($array); echo $arrayEncoded . " "; //prints - {"stuff":"things"} $arrayDecoded = json_decode($arrayEncoded); print_r($arrayDecoded); //prints - stdClass Object ( [stuff] => things )
为什么PHP把JSON对象转化为类了?不该数组吗。你看,显示json_encode,然后是json_decode,不是应该输出完全相同的结果吗?
回答
仔细看son_decode($json, $assoc, $depth)的第二个参数,资料地址
如果$assoc=false的话(默认值),将返回对象而不是数组。这样咱们才能把一个数组快速的转换成类。
如果$assoc=false的话(默认值),将返回对象而不是数组。这样咱们才能把一个数组快速的转换成类。