1

Ive got an API which shows data from power meters in my office. Its a total of four meters and the values im getting back are current consumption a total consumption on the different meters.

Im able to retrieve the respons as an JSON (I dont know if this is the correct way but Im new to APIs and its the only approach I know), and I can tell that its a multidimensional array, but I am not able to fetch the individual values. It feel like im close but I just cant get it to work.

Im trying to reach the meter called "Meter_Power_2" and "value" but no.

<?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>";

    }

?>

Currently is just looping through everything and not giving me anything back, so there obviously something wrong with my looping. Please help

EDIT: heres the respons in its entirety.

{
  "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"
}
3
  • 1
    Can you please share the output of the print_r($data);? Commented Jun 21, 2019 at 10:06
  • oh yes, sorry. updated above Commented Jun 21, 2019 at 10:08
  • yes, sorry. ive noticed that those meters are super reliable. at times they seem to be offline completely :P Commented Jun 21, 2019 at 10:10

2 Answers 2

2

Here is the snippet,

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/>';
            }
        }
    }
}

If you don't mine considering measurement hardcoded then,

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";
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

had no idea such a thing as array_walk existed, and reading up on it it seems like something really useful! learned something new today :)
0

You may try this for the simpler one:

<?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>";
    }

?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.