Saucepan
Saucepan

Reputation: 308

Fetching data from a Multidimensional Array PHP

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

Upvotes: 1

Views: 205

Answers (2)

Rahul
Rahul

Reputation: 18557

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

Upvotes: 2

Josua M C
Josua M C

Reputation: 3158

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

?>

Upvotes: 0

Related Questions