關於PHP循環讀取json資料的方法

藏色散人
發布: 2023-04-09 17:24:01
轉載
4696 人瀏覽過

推薦:《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 &#39;<br>&#39;;
        foreach($city[&#39;area&#39;] as $area){
            echo &#39;----&#39;, $area, &#39;&#39;;
			echo &#39;<br>&#39;;
        }
    }
}
exit;
登入後複製

輸出結果:

以上是關於PHP循環讀取json資料的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板