Trying to access array offset on value of type float.

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

error Trying to access array offset on value of type float

问题

  1. foreach($array["daily"]["time"] as $key => $value) {
  2. echo "<tr>";
  3. echo "<td>" . $value . "</td>";
  4. echo "<td>" . $array["daily"]["wave_height_max"][$key] . "</td>";
  5. echo "<td>" . $array["daily"]["wind_wave_height_max"][$key] . "</td>";
  6. echo "</tr>";
  7. }
英文:

I have this code:

  1. &lt;?php
  2. $url = &#39;https://marine-api.open-meteo.com/v1/marine?
  3. latitude=10.307&amp;longitude=-85.839&amp;daily=wave_height_max,wind_wave_height_max&amp;timezone=auto&#39;;
  4. $data = file_get_contents($url);
  5. $array = json_decode($data, true);
  6. echo &quot;&lt;table&gt;&quot;;
  7. foreach($array as $key =&gt; $value) {
  8. echo &quot;&lt;tr&gt;&quot;;
  9. echo &quot;&lt;td&gt;&quot; . $value[&quot;time&quot;] . &quot;&lt;/td&gt;&quot;;
  10. echo &quot;&lt;td&gt;&quot; . $value[&quot;wave_height_max&quot;] . &quot;&lt;/td&gt;&quot;;
  11. echo &quot;&lt;td&gt;&quot; . $value[&quot;wind_wave_height_max&quot;] . &quot;&lt;/td&gt;&quot;;
  12. echo &quot;&lt;/tr&gt;&quot;;
  13. }
  14. echo &quot;&lt;/table&gt;&quot;;
  15. ?&gt;

and I am getting

> Trying to access array offset on value of type float

error

The JSON is:

  1. {
  2. &quot;latitude&quot;: 10.0,
  3. &quot;longitude&quot;: -86.0,
  4. &quot;generationtime_ms&quot;: 0.6439685821533203,
  5. &quot;utc_offset_seconds&quot;: -21600,
  6. &quot;timezone&quot;: &quot;America/Costa_Rica&quot;,
  7. &quot;timezone_abbreviation&quot;: &quot;CST&quot;,
  8. &quot;daily_units&quot;: {
  9. &quot;time&quot;: &quot;iso8601&quot;,
  10. &quot;wave_height_max&quot;: &quot;m&quot;,
  11. &quot;wind_wave_height_max&quot;: &quot;m&quot;
  12. },
  13. &quot;daily&quot;: {
  14. &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;],
  15. &quot;wave_height_max&quot;: [1.34, 1.14, 1.38, 1.70, 1.84, 1.72, 1.72],
  16. &quot;wind_wave_height_max&quot;: [0.14, 0.26, 0.16, 0.40, 0.74, 0.22, 0.10]
  17. }
  18. }

答案1

得分: 1

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

  1. &lt;?php
  2. $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;';
  3. $data = file_get_contents($url);
  4. $array = json_decode($data, true);
  5. echo &quot;&lt;table&gt;&quot;
  6. foreach($array[&quot;daily&quot;][&quot;time&quot;] as $key =&gt; $value) {
  7. echo &quot;&lt;tr&gt;&quot;;
  8. echo &quot;&lt;td&gt;&quot; . $value . &quot;&lt;/td&gt;&quot;;
  9. echo &quot;&lt;td&gt;&quot; . $array[&quot;daily&quot;][&quot;wave_height_max&quot;][$key] . &quot;&lt;/td&gt;&quot;;
  10. echo &quot;&lt;td&gt;&quot; . $array[&quot;daily&quot;][&quot;wind_wave_height_max&quot;][$key] . &quot;&lt;/td&gt;&quot;;
  11. echo &quot;&lt;/tr&gt;&quot;;
  12. }
  13. echo &quot;&lt;/table&gt;&quot;;
  14. ?&gt;
英文:

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

  1. &lt;?php
  2. $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;;
  3. $data = file_get_contents($url);
  4. $array = json_decode($data, true);
  5. echo &quot;&lt;table&gt;&quot;;
  6. foreach($array[&quot;daily&quot;][&quot;time&quot;] as $key =&gt; $value) {
  7. echo &quot;&lt;tr&gt;&quot;;
  8. echo &quot;&lt;td&gt;&quot; . $value . &quot;&lt;/td&gt;&quot;;
  9. echo &quot;&lt;td&gt;&quot; . $array[&quot;daily&quot;][&quot;wave_height_max&quot;][$key] . &quot;&lt;/td&gt;&quot;;
  10. echo &quot;&lt;td&gt;&quot; . $array[&quot;daily&quot;][&quot;wind_wave_height_max&quot;][$key] . &quot;&lt;/td&gt;&quot;;
  11. echo &quot;&lt;/tr&gt;&quot;;
  12. }
  13. echo &quot;&lt;/table&gt;&quot;;
  14. ?&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:

确定