首页 > IT职业 > IT图书 > php程序遍历json数据的方法

1、json_decode是php5.2.0之后新增的一个PHP内置函数,其作用是对JSON格式的字符串进行编码。
2、接受JSON格式的字符串并且把它转换为PHP变量。
当该参数$assoc为TRUE时,将返回array,否则返回object。
语法规则:
json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
实例
<?php
$str = '{
"10924": {
"id": "10924",
"title": "天津",
"streamline_title": "狗不理",
"unit": "点",
"goods_type": "168",
"goods_type_title": "包子"
},
"10923": {
"id": "10923",
"title": "北京",
"streamline_title": "王府井",
"unit": "点",
"goods_type": "104",
"goods_type_title": "吃货天堂"
},
"11982": {
"id": "11982",
"title": "南京",
"streamline_title": "夫子庙",
"unit": "点",
"goods_type": "351",
"goods_type_title": "灯会"
}
}';
foreach (json_decode($str) as $v)
{
echo "{$v->id} {$v->title}"; //其他的一样的
}
以上就是php程序遍历json数据的方法,希望对大家有所帮助。更多php学习指路:php教程
本文转载于php中文网,如有侵犯联系作者修改。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/834/viewspace-2828110/ ,如需转载,请注明出处,否则将追究法律责任。