php教程_关于PHP循环读取json数据的方法

 所属分类:php教程

 浏览:169次-  评论: 0次-  更新时间:2022-06-14
描述:这是一篇php教程_关于PHP循环读取json数据的方法的说明内容,如果你想学习查找类似的文章,可以进入php教程获得最新优质资料。 推荐:《PHP...
这是一篇php教程_关于PHP循环读取json数据的方法的说明内容,如果你想学习查找类似的文章,可以进入php教程获得最新优质资料。

推荐:《PHP视频教程》

现有json数据如下:

[{
	"name": "北京",
	"city": [{
		"name": "北京",
		"area": ["东城区", "西城区", "崇文区", "宣武区", "朝阳区", "丰台区", "石景山区", "海淀区", "门头沟区", "房山区", "通州区", "顺义区", "昌平区", "大兴区", "平谷区", "怀柔区", "密云县", "延庆县"]
	}]
}, {
	"name": "天津",
	"city": [{
		"name": "天津",
		"area": ["和平区", "河东区", "河西区", "南开区", "河北区", "红桥区", "塘沽区", "汉沽区", "大港区", "东丽区", "西青区", "津南区", "北辰区", "武清区", "宝坻区", "宁河县", "静海县", "蓟 县"]
	}]
}]

使用PHP的foreach进行循环输出:

$str = '[{"name":"北京","city":[{"name":"北京","area":["东城区","西城区","崇文区","宣武区","朝阳区","丰台区","石景山区","海淀区","门头沟区","房山区","通州区","顺义区","昌平区","大兴区","平谷区","怀柔区","密云县","延庆县"]}]},{"name":"天津","city":[{"name":"天津","area":["和平区","河东区","河西区","南开区","河北区","红桥区","塘沽区","汉沽区","大港区","东丽区","西青区","津南区","北辰区","武清区","宝坻区","宁河县","静海县","蓟 县"]}]}]';
$provinces = json_decode($str, true);
foreach($provinces as $province){
    echo $province['name'], '';
    foreach($province['city'] as $city){
        echo ':', $city['name'], '';
		echo '<br>';
        foreach($city['area'] as $area){
            echo '----', $area, '';
			echo '<br>';
        }
    }
}
exit;

输出结果:

以上就是关于PHP循环读取json数据的方法的详细内容,更多请关注zzsucai.com其它相关文章!

 标签:
积分说明:注册即送10金币,每日签到可获得更多金币,成为VIP会员可免金币下载! 充值积分充值会员更多说明»

讨论这个素材(0)回答他人问题或分享使用心得奖励金币

〒_〒 居然一个评论都没有……

表情  文明上网,理性发言!