Trying to access array offset on value of type float.

huangapple go评论93阅读模式
英文:

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:

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

echo &quot;&lt;table&gt;&quot;;

foreach($array as $key =&gt; $value) {
    echo &quot;&lt;tr&gt;&quot;;
    echo &quot;&lt;td&gt;&quot; . $value[&quot;time&quot;] . &quot;&lt;/td&gt;&quot;;
    echo &quot;&lt;td&gt;&quot; . $value[&quot;wave_height_max&quot;] . &quot;&lt;/td&gt;&quot;;
    echo &quot;&lt;td&gt;&quot; . $value[&quot;wind_wave_height_max&quot;] . &quot;&lt;/td&gt;&quot;;
    echo &quot;&lt;/tr&gt;&quot;;
}
echo &quot;&lt;/table&gt;&quot;;
?&gt;

and I am getting

> Trying to access array offset on value of type float

error

The JSON is:

{
	&quot;latitude&quot;: 10.0,
	&quot;longitude&quot;: -86.0,
	&quot;generationtime_ms&quot;: 0.6439685821533203,
	&quot;utc_offset_seconds&quot;: -21600,
	&quot;timezone&quot;: &quot;America/Costa_Rica&quot;,
	&quot;timezone_abbreviation&quot;: &quot;CST&quot;,
	&quot;daily_units&quot;: {
		&quot;time&quot;: &quot;iso8601&quot;,
		&quot;wave_height_max&quot;: &quot;m&quot;,
		&quot;wind_wave_height_max&quot;: &quot;m&quot;
	},
	&quot;daily&quot;: {
		&quot;time&quot;: [&quot;2023-03-09&quot;, &quot;2023-03-10&quot;, &quot;2023-03-11&quot;, &quot;2023-03-12&quot;, &quot;2023-03-13&quot;, &quot;2023-03-14&quot;, &quot;2023-03-15&quot;],
		&quot;wave_height_max&quot;: [1.34, 1.14, 1.38, 1.70, 1.84, 1.72, 1.72],
		&quot;wind_wave_height_max&quot;: [0.14, 0.26, 0.16, 0.40, 0.74, 0.22, 0.10]
	}
}

答案1

得分: 1

你应该更新你的代码以从 daily 中访问 wave_height_maxwind_wave_height_max 的值:

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

echo &quot;&lt;table&gt;&quot;

foreach($array[&quot;daily&quot;][&quot;time&quot;] as $key =&gt; $value) {
    echo &quot;&lt;tr&gt;&quot;;
    echo &quot;&lt;td&gt;&quot; . $value . &quot;&lt;/td&gt;&quot;;
    echo &quot;&lt;td&gt;&quot; . $array[&quot;daily&quot;][&quot;wave_height_max&quot;][$key] . &quot;&lt;/td&gt;&quot;;
    echo &quot;&lt;td&gt;&quot; . $array[&quot;daily&quot;][&quot;wind_wave_height_max&quot;][$key] . &quot;&lt;/td&gt;&quot;;
    echo &quot;&lt;/tr&gt;&quot;;
}
echo &quot;&lt;/table&gt;&quot;;
?&gt;
英文:

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

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

echo &quot;&lt;table&gt;&quot;;

foreach($array[&quot;daily&quot;][&quot;time&quot;] as $key =&gt; $value) {
    echo &quot;&lt;tr&gt;&quot;;
    echo &quot;&lt;td&gt;&quot; . $value . &quot;&lt;/td&gt;&quot;;
    echo &quot;&lt;td&gt;&quot; . $array[&quot;daily&quot;][&quot;wave_height_max&quot;][$key] . &quot;&lt;/td&gt;&quot;;
    echo &quot;&lt;td&gt;&quot; . $array[&quot;daily&quot;][&quot;wind_wave_height_max&quot;][$key] . &quot;&lt;/td&gt;&quot;;
    echo &quot;&lt;/tr&gt;&quot;;
}
echo &quot;&lt;/table&gt;&quot;;
?&gt;

huangapple
  • 本文由 发表于 2023年3月9日 22:40:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定