英文:
error Trying to access array offset on value of type float
问题
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>";
}
英文:
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]
}
}
答案1
得分: 1
你应该更新你的代码以从 daily
中访问 wave_height_max
和 wind_wave_height_max
的值:
<?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>";
?>
英文:
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>";
?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论