本文目录一览:
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 数组
?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
?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时就是转换成数组