I have defined PHP code as dynamically. How to push tempMax variable as today and separate tomorrow? We have two headers and two Min, Max. It means 2 index (i) from 0 to 1 and each day Min, Max not only one.
You don’t need separate variables like tempMax_info_index_0, tempMax_info_index_1, etc. That defeats the point of using a loop.
The correct way is one set of variables ($label, $tempMax, $tempMin, $icon) with one HTML template repeated by the loop (index 0 = today, 1 = tomorrow, 2 = next day, etc.)
Right now your problem is that you output two containers inside one loop iteration, so both use the same values. Instead, output one container per iteration:
As I mentioned, the snippet was untested and assumes the API response has the expected structure. That error means you’re accessing array offsets on a string, so this is a PHP/data issue, not HTML. In practice it usually means one of these:
$data is still a JSON string (not decoded with json_decode(..., true)), or
OpenWeather returned an error response, so $data['daily'] isn’t an array, or
you’re looping the wrong level of the response.
Before the loop, quickly verify that $data is an array and that $data['daily'] exists and is an array.
If you paste the output var_dump($data) that will help us debug.
Hi, the problem is just that sometimes the API does not return daily (for example when the quota is hit, or OpenWeather returns an error message). Then your loop runs on invalid data and PHP crashes.
Just guard before the foreach:
if (!isset($data['daily']) || !is_array($data['daily'])) {
// API call failed or no forecast data – don't output anything
return;
}
This is not likely a WordPress theme issue. That error will happen when PHP tries to do something like:
$day['temp']['max']
but $day is actually a string, not an array.
From your var_dump, I only see lat, lon, timezone, current, etc. But the output looks cut off and I don’t see daily. That could mean that you are getting some valid data back (e.g. current), but the crash happens when the code reaches the daily loop and the response isn’t the expected daily-forecast structure .
Your code will stop if data are not there. So, I guess this log validation should be placed.
if (!isset($data['daily']) || !is_array($data['daily'])) {
echo "API call failed or no forecast data";
return;
}
Also, when I test a error comes within MAX line. If we place 3 it will be thrown an error. If we have 2 as Max, it will not modify container as all the time it will be container 2 which should be container1 when index is 0. Index 0+1 =1. Next loop Index 1 + 1=2 and stop the loop.
<div class='container".max(2, $index+1)."'>
I think it should be more strict instruction inside PHP. As I understand PHP server can sometimes lose .max(2. Maybe I have Cache issue as WordPress lacks updates.