-1

I have this code:

<?php
$url = 'https://marine-api.open-meteo.com/v1/marine? 
latitude=10.307&longitude=-85.839&daily=wave_height_max,wind_wave_height_max&timezone=auto';
$data = file_get_contents($url);
$array = json_decode($data, true);

echo "<table>";

foreach($array as $key => $value) {
    echo "<tr>";
    echo "<td>" . $value["time"] . "</td>";
    echo "<td>" . $value["wave_height_max"] . "</td>";
    echo "<td>" . $value["wind_wave_height_max"] . "</td>";
    echo "</tr>";
}
echo "</table>";
?>

and I am getting

Trying to access array offset on value of type float

error

The JSON is:

{
    "latitude": 10.0,
    "longitude": -86.0,
    "generationtime_ms": 0.6439685821533203,
    "utc_offset_seconds": -21600,
    "timezone": "America/Costa_Rica",
    "timezone_abbreviation": "CST",
    "daily_units": {
        "time": "iso8601",
        "wave_height_max": "m",
        "wind_wave_height_max": "m"
    },
    "daily": {
        "time": ["2023-03-09", "2023-03-10", "2023-03-11", "2023-03-12", "2023-03-13", "2023-03-14", "2023-03-15"],
        "wave_height_max": [1.34, 1.14, 1.38, 1.70, 1.84, 1.72, 1.72],
        "wind_wave_height_max": [0.14, 0.26, 0.16, 0.40, 0.74, 0.22, 0.10]
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Mariano
  • 17
  • 3
  • 1
    The first item of the resulting array, that you are looping over, has the key `latitude` and the value `10.0`. Trying to access `$value["time"]` of course makes no sense with that value. – CBroe Mar 09 '23 at 14:50
  • Well you've got a single object there, not an array...so you can't loop it the way you're expecting. Looks like you actually wanted to loop the "daily" values, but that's not an array either, it's an object which contains a set of separate arrays. – ADyson Mar 09 '23 at 14:50
  • Have a read of [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) and then make sure you understand what the JSON you're looking at there actually represents. If you find it easier to read PHP array syntax, then run `var_export($array);` after you've created it, so you understand the structure you're dealing with. – ADyson Mar 09 '23 at 14:51
  • @ADyson, Mariano has always been calling `json_decode($data, true)` -- this will be creating arrays, not objects. The json encoded format, of course, looks like objects. – mickmackusa Mar 10 '23 at 02:19
  • @mickmackusa indeed. my comment referred to the JSON structure, not the resulting php variable, probably should have made that clearer :-) – ADyson Mar 10 '23 at 08:01

1 Answers1

1

You should update your code to access wave_height_max and wind_wave_height_max values from the daily :

<?php
$url = 'https://marine-api.open-meteo.com/v1/marine?latitude=10.307&longitude=-85.839&daily=wave_height_max,wind_wave_height_max&timezone=auto';
$data = file_get_contents($url);
$array = json_decode($data, true);

echo "<table>";

foreach($array["daily"]["time"] as $key => $value) {
    echo "<tr>";
    echo "<td>" . $value . "</td>";
    echo "<td>" . $array["daily"]["wave_height_max"][$key] . "</td>";
    echo "<td>" . $array["daily"]["wind_wave_height_max"][$key] . "</td>";
    echo "</tr>";
}
echo "</table>";
?>
Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68