php程序如何遍历json数据

php程序如何遍历json数据

这篇文章将为大家详细讲解有关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数据”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。