我是第一次使用JSON数据而且我有一些PHP来获取如下所示的一些JSON数据(除了 body 中有数百个 measuregrps >)。
$json = file_get_contents("http://wbsapi.withings.net/measure?action=getmeasures");
$json_o = json_decode($json);
如何使用foreach为type = 1创建一维值数组?
{
"status": 0,
"body": {
"updatetime": 1249409679,
"measuregrps": [
{
"grpid": 2909,
"attrib": 0,
"date": 1222930968,
"category": 1,
"measures": [
{
"value": 79300,
"type": 1,
"unit": -3
},
{
"value": 652,
"type": 5,
"unit": -1
},
{
"value": 178,
"type": 6,
"unit": -1
},
{
"value": 14125,
"type": 8,
"unit": -3
}
]
},
{
"grpid": 2908,
"attrib": 0,
"date": 1222930968,
"category": 1,
"measures": [
{
"value": 78010,
"type": 1,
"unit": -3
}
]
},
{
"grpid": 2907,
"attrib": 0,
"date": 1222930968,
"category": 1,
"measures": [
{
"value": 77300,
"type": 1,
"unit": -3
},
{
"value": 678,
"type": 5,
"unit": -1
}
]
},
]
}
}
答案 0 :(得分:2)
$json_o = json_decode($json,true);
$result = array();
foreach ($json_o['body']['measuregrps'] as $measuregrp)
foreach ($measuregrp['measures'] as $measure)
if ($measure['type'] == 1)
$result []= $measure['value'];
答案 1 :(得分:0)
像
这样的东西$values = array();
foreach($json_o->body->measuregrps as $group){
foreach($group->measures as $measure){
if($measure->type == 1){
$values[] = $measure->value;
}
}
}
print_r($values);
会做