我有一个API,可以显示我办公室中电表的数据。它总共有四米,而即时取回的值是电流消耗,是不同米的总消耗。
我能够将响应作为JSON检索(我不知道这是否是正确的方法,但是我是API的新手,这是我知道的唯一方法),我可以说它是多维数组,但我不是能够获取单个值。感觉好像很接近,但我无法正常工作。
我正在尝试到达名为“ Meter_Power_2”和“ value”的仪表,但没有。
<?php
$url = "http://95.209.138.71:12380/api/v1/measurements";
$data = json_decode(file_get_contents($url), TRUE);
print_r($data);
?>
<br>
<?php
foreach($data as $row){
echo $row['Meter_2_Power']['Value']."<br>";
}
?>
当前只是循环浏览所有内容,而没有给我任何回报,因此显然我的循环存在问题。请帮助
编辑:这是完整的响应。
{
"measurements": [
{
"4000_RelayStatus": {
"Timestamp": "2019-06-21T08:51:06",
"Value": 0.0
},
"Address": "4000"
},
{
"4000_Power": {
"Timestamp": "2019-06-21T08:51:06",
"Value": 0.0
},
"Address": "4000"
},
{
"4001_RelayStatus": {
"Timestamp": "2019-06-21T10:15:47",
"Value": 0.0
},
"Address": "4001"
},
{
"4001_Power": {
"Timestamp": "2019-06-21T10:15:47",
"Value": 0.0
},
"Address": "4001"
},
{
"1200_RelayStatus": {
"Timestamp": "2018-04-23T12:04:14",
"Value": 0.0
},
"Address": "1200"
},
{
"1200_Power": {
"Timestamp": "2018-04-23T12:04:14",
"Value": 0.0
},
"Address": "1200"
},
{
"1100_RelayStatus": {
"Timestamp": "2018-03-28T15:56:49",
"Value": 0.0
},
"Address": "1100"
},
{
"1100_Power": {
"Timestamp": "2018-03-28T15:56:49",
"Value": 0.0
},
"Address": "1100"
},
{
"3000_RelayStatus": {
"Timestamp": "2018-03-27T20:13:18",
"Value": 0.0
},
"Address": "3000"
},
{
"3000_Power": {
"Timestamp": "2018-03-27T20:13:18",
"Value": 0.0
},
"Address": "3000"
},
{
"3500_RelayStatus": {
"Timestamp": "2018-04-25T11:19:54",
"Value": 0.0
},
"Address": "3500"
},
{
"3500_Power": {
"Timestamp": "2018-04-25T11:19:54",
"Value": 0.0
},
"Address": "3500"
},
{
"2500_RelayStatus": {
"Timestamp": "2018-04-25T16:30:35",
"Value": 0.0
},
"Address": "2500"
},
{
"2500_Power": {
"Timestamp": "2018-04-25T16:30:36",
"Value": 0.0
},
"Address": "2500"
},
{
"Address": "2600",
"Meter_1_Power": {
"Timestamp": "2019-06-21T11:44:42",
"Value": 2694.54
}
},
{
"Address": "2600",
"Meter_2_Power": {
"Timestamp": "2019-06-21T11:43:59",
"Value": 48.89
}
},
{
"Address": "2600",
"Meter_1_Total": {
"Timestamp": "2019-06-21T11:43:40",
"Value": 21716.08
}
},
{
"Address": "2600",
"Meter_2_Total": {
"Timestamp": "2019-06-21T11:29:27",
"Value": 1378.03
}
},
{
"Address": "2500",
"Meter_4_Power": {
"Timestamp": "2019-06-21T11:44:58",
"Value": 43.22
}
},
{
"Address": "2500",
"Meter_3_Power": {
"Timestamp": "2019-06-21T11:44:58",
"Value": 3508.51
}
},
{
"Address": "2500",
"Meter_4_Total": {
"Timestamp": "2019-06-21T11:38:16",
"Value": 966.34
}
},
{
"Address": "2500",
"Meter_3_Total": {
"Timestamp": "2019-06-21T11:43:19",
"Value": 39735.44
}
},
{
"2600_RelayStatus": {
"Timestamp": "2018-04-25T16:20:51",
"Value": 0.0
},
"Address": "2600"
},
{
"2600_Power": {
"Timestamp": "2018-04-25T16:20:51",
"Value": 0.0
},
"Address": "2600"
}
],
"result": "OK"
}
答案 0 :(得分:1)
这是摘要,
foreach ($arr as $key => $value) {
if (is_array($value)) {
foreach ($value as $value1) {
if (!empty($value1['Meter_2_Power']['Value'])) {
echo $value1['Meter_2_Power']['Value'] . '<br/>';
}
}
}
}
如果您不考虑对编码进行硬编码,
foreach ($arr['measurements'] as $key => $value) {
if (!empty($value['Meter_2_Power']['Value'])) {
echo $value['Meter_2_Power']['Value'] . "\n";
}
}
AND
array_walk($arr['measurements'], function($value, $key){
if (!empty($value['Meter_2_Power']['Value'])) {
echo $value['Meter_2_Power']['Value'] . "\n";
}
});
答案 1 :(得分:0)
您可以尝试以下一种简单方法:
<?php
$url = "http://95.209.138.71:12380/api/v1/measurements";
$data = json_decode(file_get_contents($url), TRUE);
?>
<br>
<?php
foreach($data['measurements'] as $row){
if(isset($row['Meter_2_Power'])) echo $row['Meter_2_Power']['Value']."<br>";
}
?>