dtuct88226 2016-12-15 03:57
浏览 100

在PHP JSON请求中使用“0”

I'm trying to create a PHP page that pulls data from Forecast.io's API. For the daily view, they give the days up as numbers. I want info from today, so the number is a "0." My code looks like this:

<?php

echo '<h4>', round(($data->daily->data->0->apparentTemperatureMax)*5/9) , '&deg;F</h4>';
echo '<h2>', round(($data->currently->apparentTemperature)*5/9) , '&deg;F</h2>'; 
echo '<h4>', round(($data->daily->data->0->apparentTemperatureMin)*5/9) , '&deg;F</h4>'; ?>`

Where it states the Min and Max temperature, the "0" is a part of the JSON format that forecast.io uses. However, PHP throws an error whenever I try this. The error:

Parse error: syntax error, unexpected '0' (T_LNUMBER), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in C:\wamp64\www\PHP Weather Widget\index.php on line 35

Is there any way to use the zero, or do I need to parse the data using another method to avoid this?

A sample file of the JSON data returned:https://darksky.net/dev/docs/forecast can be entered into this site: http://jsonviewer.stack.hu/ to view a tree view of the data returned. You can see the format by going to "daily->data."

Any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • dongmei5168 2016-12-15 04:03
    关注

    Instead of doing $data->daily->data->0->apparentTemperatureMax try doing $data['daily']['data'][0]['apparentTemperatureMax']. $data is an array, not an object.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度