本文目录一览:
- 1、php如何获取json中数据
- 2、php 循环遍历json数据
- 3、php 循环输出 json 数组
- 4、php 读取 json 里面的字段内容
- 5、谁能帮我看一下这个json怎么用php循环出来!
php如何获取json中数据
可以用$obj=key 的方式直接读取 也可以先转换为数组 用遍历数组的方法读取
?php
$json = '{"a":100,"b":200,"c":300,"d":400,"e":500}';
//首先将json字符串转换成关联数组
$arr=json_decode($json, true);
//然后循环读取数据
foreach($arr as $item){
echo $item;
echo "br/";
}
?
运行结果:
100
200
300
400
500
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}"; //其他的一样的
}
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":""}]';-phpjson循环
$arr=json_decode($Tdata,true);
//var_dump($arr);
echo stripcslashes ($arr[0][A_Name]);
?
上面的方法可以打印输出,json_decode($Tdata)输出的结果是一个对应,而json_decode($Tdata,true)输出的是一个数组
php 读取 json 里面的字段内容
示例代码如下:
?php
header("Content-type: text/html; charset=utf-8");
$postArray = '[{"count":{"左脑":2,"右脑":3,"全脑":2,"全脑1":1},"score":{"左脑":8,"右脑":14,"全脑":15,"全脑1":8},"pingjun":{"左脑":4,"右脑":4.6666666666667,"全脑":7.5,"全脑1":8}}]';-phpjson循环
$de_json = json_decode($postArray, true);
echo $de_json[0]['pingjun']['左脑'];
?
json 数组结构如图:
谁能帮我看一下这个json怎么用php循环出来!
$json = '[{"name":"北京","city":[{"name":"北京","area":["东城区","西城区","崇文区","宣武区","朝阳区","丰台区","石景山区","海淀区","门头沟区","房山区","通州区","顺义区","昌平区","大兴区","平谷区","怀柔区","密云县","延庆县"]}]},{"name":"天津","city":[{"name":"天津","area":["和平区","河东区","河西区","南开区","河北区","红桥区","塘沽区","汉沽区","大港区","东丽区","西青区","津南区","北辰区","武清区","宝坻区","宁河县","静海县","蓟 县"]}]}]';-phpjson循环
$data = json_decode($json,true);
echo 'pre';print_r($data);
打印出来看到数组按使用需要去做循环