本文目录一览:
php循环读取json
?php
$str = '[{"name":"北京","city":[{"name":"北京","area":["东城区","西城区","崇文区","宣武区","朝阳区","丰台区","石景山区","海淀区","门头沟区","房山区","通州区","顺义区","昌平区","大兴区","平谷区","怀柔区","密云县","延庆县"]}]},{"name":"天津","city":[{"name":"天津","area":["和平区","河东区","河西区","南开区","河北区","红桥区","塘沽区","汉沽区","大港区","东丽区","西青区","津南区","北辰区","武清区","宝坻区","宁河县","静海县","蓟 县"]}]}]';
$provinces = json_decode($str, true);
foreach($provinces as $province){
echo $province['name'], 'br/';
foreach($province['city'] as $city){
echo '--', $city['name'], 'br/';
foreach($city['area'] as $area){
echo '----', $area, 'br/';
}
}
}
exit;
Php json循环解析
json_encode(),转换成json格式
json_decode(),将json转换成对象,第二个参数为true时就是转换成数组
php 循环输出 json 数组
?php
$Tdata='[{"A_Name":"a1","ICNo":"","A_ContNo":"","A_QQNo":""},{"A_Name":"a2","ICNo":"","A_ContNo":"","A_QQNo":""},{"A_Name":"a3","ICNo":"","A_ContNo":"","A_QQNo":""}]';
$arr=json_decode($Tdata,true);
//var_dump($arr);
echo stripcslashes ($arr[0][A_Name]);
?
上面的方法可以打印输出,json_decode($Tdata)输出的结果是一个对应,而json_decode($Tdata,true)输出的是一个数组
PHP循环读取JSON
首先你要把JSON数据转成数组,因为在php中JSON就是一个字符串。
$jsonData = '{"list": [{
"goodsid": 4290,
"post": [{
"param": "uin",
"name": "账号"
}, {
"param": "goodsid",
"name": "商品ID"
}, {
"param": "number",
"name": "下单数量"
}]
}, ';
// 你的JSON数据有点乱,我复制的可能不对
$aryData = json_decode($jsonData, true); // 转码为数组
var_dump($aryData); // 打印
foreach ($aryData as $item) {
var_dump($item); // 打印每个条目
}
php 循环遍历json数据
$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}"; //其他的一样的
}