我想知道是否有人可以帮忙。我有一个来自API的JSON提要,我永远无法弄清楚获得所需信息的级别吗?
JSON低于
{
"sourceId": "1234",
"sourceType": "MONITOR",
"searchMeta": {
"startTimestamp": 1462361513,
"endTimestamp": 1462508560,
"maxResults": 10000,
"metricType": [
"clients"
],
"family": [
"Product"
],
"metricsInterval": "M5"
},
"clients": {
"One": [
{
"timestamp": 1462361400,
"avg": 2,
"min": 1,
"max": 3,
"probes": 6,
"samples": 3,
"sources": [
"123",
"234",
"345",
"456",
"567",
"789"
]
},
我想将Probes值和Samples值变为变量
foreach($json['clients'] as $range => $product){
echo $product['timestamp']." Probes: ".$product['probes']." Samples: ".$product['samples']." <br>";
}
提前致谢
答案 0 :(得分:1)
我假设您有一系列客户端(因此循环)。因此,您需要循环每个客户端,因为它是array
object
的{{1}}。
如果您想象它,它看起来像这样:
CLIENTS = ARRAY(
1 => ARRAY( OBJECT{} ), // you want the OBJECT{}
....etc
)
因此,下面的循环将为您提供。
$data = json_decode($json, TRUE);
foreach($data['clients'] as $range => $product) {
foreach($product as $element){
echo $element['timestamp']." Probes: ".$element['probes']." Samples: ".$element['samples']." <br>";
}
}
返回以下内容:
1462361400 Probes: 6 Samples: 3 <br>
答案 1 :(得分:0)
OP的要求:
我想把Probes值和Samples值变成a 变量
简单地将您的json文件命名为$json
。
$arr = json_decode($json);
echo $samples = $arr->clients->One[0]->samples; //3
echo $probes = $arr->clients->One[0]->probes; //6
答案 2 :(得分:0)
试试这个。
$data = json_decode($json,true);
foreach($data['clients'] as $client){
echo $client['timestamp']." Probes: ".$client['probes']." Samples: ".$client['samples']." <br>";
}