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.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题