2014-09-21 140 views
-1

我想在PHP中创建一个数组,但我不知道我做错了什么。下面似乎并不奏效。当我创建一个我可以访问它的变量时,json是绝对可用的。然而,该阵列似乎并没有工作。创建PHP阵列

for ($x=0; $x<=7; $x++){ 

$forecasetp[] = array(); 

    $forecastp[x] = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[x]->{'title'}; 

为了简化我想更换一个循环被解析

$forecastp2 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[1]->{'title'}; 
    $forecastt2 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[1]->{'fcttext'}; 

    $forecastp3 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[2]->{'title'}; 
    $forecastt3 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[2]->{'fcttext'}; 

    $forecastp4 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[3]->{'title'}; 
    $forecastt4 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[3]->{'fcttext'}; 

    $forecastp5 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[4]->{'title'}; 
      $forecastt5 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[4]->{'fcttext'}; 

     $forecastp6 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[5]->{'title'}; 
     $forecastt6 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[5]->{'fcttext'}; 

     $forecastp7 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[6]->{'title'}; 
     $forecastt7 = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}[6]->{'fcttext'}; 

JSON如下:

{ 
    "response": { 
    "version":"0.1", 
    "termsofService":"http://www.wunderground.com/weather/api/d/terms.html", 
    "features": { 
    "forecast10day": 1 
    } 
    } 
     , 
    "forecast":{ 
     "txt_forecast": { 
     "date":"5:08 PM EDT", 
     "forecastday": [ 
     { 
     "period":0, 
     "icon":"partlycloudy", 
     "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif", 
     "title":"Sunday", 
     "fcttext":"Partly cloudy. Lows overnight in the upper 50s.", 
     "fcttext_metric":"Partly cloudy. Low 16C.", 
     "pop":"10" 
     } 
     , 
     { 
     "period":1, 
     "icon":"nt_partlycloudy", 
     "icon_url":"http://icons.wxug.com/i/c/k/nt_partlycloudy.gif", 
     "title":"Sunday Night", 
     "fcttext":"Partly cloudy skies. Low 59F. Winds WSW at 5 to 10 mph.", 
     "fcttext_metric":"Partly cloudy skies. Low 16C. Winds WSW at 10 to 15 km/h.", 
     "pop":"10" 
     } 
     , 
     { 
     "period":2, 
     "icon":"clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
     "title":"Monday", 
     "fcttext":"Sunny. High 69F. Winds WNW at 10 to 20 mph.", 
     "fcttext_metric":"A mainly sunny sky. High 21C. Winds WNW at 15 to 30 km/h.", 
     "pop":"0" 
     } 
     , 
     { 
     "period":3, 
     "icon":"nt_clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif", 
     "title":"Monday Night", 
     "fcttext":"Clear. Low 46F. Winds NW at 5 to 10 mph.", 
     "fcttext_metric":"A mostly clear sky. Low 9C. Winds NW at 10 to 15 km/h.", 
     "pop":"0" 
     } 
     , 
     { 
     "period":4, 
     "icon":"partlycloudy", 
     "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif", 
     "title":"Tuesday", 
     "fcttext":"Intervals of clouds and sunshine. High 72F. Winds light and variable.", 
     "fcttext_metric":"Intervals of clouds and sunshine. High 22C. Winds light and variable.", 
     "pop":"0" 
     } 
     , 
     { 
     "period":5, 
     "icon":"nt_partlycloudy", 
     "icon_url":"http://icons.wxug.com/i/c/k/nt_partlycloudy.gif", 
     "title":"Tuesday Night", 
     "fcttext":"Partly cloudy skies. Low 51F. Winds light and variable.", 
     "fcttext_metric":"Partly cloudy skies. Low 11C. Winds light and variable.", 
     "pop":"10" 
     } 
     , 
     { 
     "period":6, 
     "icon":"clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
     "title":"Wednesday", 
     "fcttext":"Sunshine and some clouds. High 73F. Winds ENE at 5 to 10 mph.", 
     "fcttext_metric":"Sunny, along with a few afternoon clouds. High 23C. Winds ENE at 10 to 15 km/h.", 
     "pop":"10" 
     } 
     , 
     { 
     "period":7, 
     "icon":"nt_clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif", 
     "title":"Wednesday Night", 
     "fcttext":"Mostly clear skies. Low 54F. Winds ENE at 5 to 10 mph.", 
     "fcttext_metric":"Mostly clear skies. Low 13C. Winds ENE at 10 to 15 km/h.", 
     "pop":"10" 
     } 
     , 
     { 
     "period":8, 
     "icon":"partlycloudy", 
     "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif", 
     "title":"Thursday", 
     "fcttext":"Intervals of clouds and sunshine. High 72F. Winds ENE at 5 to 10 mph.", 
     "fcttext_metric":"Partly cloudy. High 23C. Winds ENE at 10 to 15 km/h.", 
     "pop":"20" 
     } 
     , 
     { 
     "period":9, 
     "icon":"nt_clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif", 
     "title":"Thursday Night", 
     "fcttext":"Mainly clear. Low 52F. Winds light and variable.", 
     "fcttext_metric":"Mainly clear. Low 12C. Winds light and variable.", 
     "pop":"10" 
     } 
     , 
     { 
     "period":10, 
     "icon":"clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
     "title":"Friday", 
     "fcttext":"Sunny skies. High 78F. Winds light and variable.", 
     "fcttext_metric":"Some clouds in the morning will give way to mainly sunny skies for the afternoon. High 26C. Winds light and variable.", 
     "pop":"0" 
     } 
     , 
     { 
     "period":11, 
     "icon":"nt_clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif", 
     "title":"Friday Night", 
     "fcttext":"Clear skies. Low 53F. Winds light and variable.", 
     "fcttext_metric":"A mostly clear sky. Low 12C. Winds light and variable.", 
     "pop":"0" 
     } 
     , 
     { 
     "period":12, 
     "icon":"clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
     "title":"Saturday", 
     "fcttext":"Mainly sunny. High 81F. Winds light and variable.", 
     "fcttext_metric":"Sunny skies. High 28C. Winds light and variable.", 
     "pop":"0" 
     } 
     , 
     { 
     "period":13, 
     "icon":"nt_clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif", 
     "title":"Saturday Night", 
     "fcttext":"Clear skies. Low 57F. Winds light and variable.", 
     "fcttext_metric":"Clear skies. Low 14C. Winds light and variable.", 
     "pop":"0" 
     } 
     , 
     { 
     "period":14, 
     "icon":"clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
     "title":"Sunday", 
     "fcttext":"Sunny. High near 80F. Winds E at 5 to 10 mph.", 
     "fcttext_metric":"Mainly sunny. High 27C. Winds E at 10 to 15 km/h.", 
     "pop":"0" 
     } 
     , 
     { 
     "period":15, 
     "icon":"nt_clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif", 
     "title":"Sunday Night", 
     "fcttext":"A few clouds overnight. Low 58F. Winds light and variable.", 
     "fcttext_metric":"Clear to partly cloudy. Low around 15C. Winds SSW at 10 to 15 km/h.", 
     "pop":"20" 
     } 
     , 
     { 
     "period":16, 
     "icon":"partlycloudy", 
     "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif", 
     "title":"Monday", 
     "fcttext":"Mostly cloudy skies early will become partly cloudy later in the day. High 79F. Winds light and variable.", 
     "fcttext_metric":"Mostly cloudy skies early, then partly cloudy in the afternoon. High 27C. Winds light and variable.", 
     "pop":"20" 
     } 
     , 
     { 
     "period":17, 
     "icon":"nt_mostlycloudy", 
     "icon_url":"http://icons.wxug.com/i/c/k/nt_mostlycloudy.gif", 
     "title":"Monday Night", 
     "fcttext":"Partly cloudy skies in the evening, then becoming cloudy overnight. Low 61F. Winds light and variable.", 
     "fcttext_metric":"Partly cloudy skies during the evening will give way to cloudy skies overnight. Low 17C. Winds light and variable.", 
     "pop":"20" 
     } 
     , 
     { 
     "period":18, 
     "icon":"chancerain", 
     "icon_url":"http://icons.wxug.com/i/c/k/chancerain.gif", 
     "title":"Tuesday", 
     "fcttext":"Cloudy with occasional light rain. High 73F. Winds NE at 5 to 10 mph. Chance of rain 60%.", 
     "fcttext_metric":"Cloudy with occasional light rain. High 23C. Winds NE at 10 to 15 km/h. Chance of rain 60%.", 
     "pop":"60" 
     } 
     , 
     { 
     "period":19, 
     "icon":"nt_chancerain", 
     "icon_url":"http://icons.wxug.com/i/c/k/nt_chancerain.gif", 
     "title":"Tuesday Night", 
     "fcttext":"Light rain transitioning to a few showers late. Low near 55F. Winds NE at 5 to 10 mph. Chance of rain 60%.", 
     "fcttext_metric":"Occasional light rain tapering to a few showers late. Low 14C. Winds NE at 10 to 15 km/h. Chance of rain 60%.", 
     "pop":"60" 
     } 
     ] 
     }, 
     "simpleforecast": { 
     "forecastday": [ 
     {"date":{ 
    "epoch":"1411340400", 
    "pretty":"7:00 PM EDT on September 21, 2014", 
    "day":21, 
    "month":9, 
    "year":2014, 
    "yday":263, 
    "hour":19, 
    "min":"00", 
    "sec":0, 
    "isdst":"1", 
    "monthname":"September", 
    "monthname_short":"Sep", 
    "weekday_short":"Sun", 
    "weekday":"Sunday", 
    "ampm":"PM", 
    "tz_short":"EDT", 
    "tz_long":"America/New_York" 
}, 
     "period":1, 
     "high": { 
     "fahrenheit":"77", 
     "celsius":"24" 
     }, 
     "low": { 
     "fahrenheit":"59", 
     "celsius":"15" 
     }, 
     "conditions":"Partly Cloudy", 
     "icon":"partlycloudy", 
     "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif", 
     "skyicon":"", 
     "pop":10, 
     "qpf_allday": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_day": { 
     "in": null, 
     "mm": null 
     }, 
     "qpf_night": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "snow_allday": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_day": { 
     "in": null, 
     "cm": null 
     }, 
     "snow_night": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "maxwind": { 
     "mph": 3, 
     "kph": 5, 
     "dir": "", 
     "degrees": 0 
     }, 
     "avewind": { 
     "mph": 0, 
     "kph": 0, 
     "dir": "", 
     "degrees": 0 
     }, 
     "avehumidity": 73, 
     "maxhumidity": 0, 
     "minhumidity": 0 
     } 
     , 
     {"date":{ 
    "epoch":"1411426800", 
    "pretty":"7:00 PM EDT on September 22, 2014", 
    "day":22, 
    "month":9, 
    "year":2014, 
    "yday":264, 
    "hour":19, 
    "min":"00", 
    "sec":0, 
    "isdst":"1", 
    "monthname":"September", 
    "monthname_short":"Sep", 
    "weekday_short":"Mon", 
    "weekday":"Monday", 
    "ampm":"PM", 
    "tz_short":"EDT", 
    "tz_long":"America/New_York" 
}, 
     "period":2, 
     "high": { 
     "fahrenheit":"69", 
     "celsius":"21" 
     }, 
     "low": { 
     "fahrenheit":"46", 
     "celsius":"8" 
     }, 
     "conditions":"Clear", 
     "icon":"clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
     "skyicon":"", 
     "pop":0, 
     "qpf_allday": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_day": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_night": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "snow_allday": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_day": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_night": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "maxwind": { 
     "mph": 20, 
     "kph": 32, 
     "dir": "WNW", 
     "degrees": 300 
     }, 
     "avewind": { 
     "mph": 13, 
     "kph": 21, 
     "dir": "WNW", 
     "degrees": 300 
     }, 
     "avehumidity": 55, 
     "maxhumidity": 0, 
     "minhumidity": 0 
     } 
     , 
     {"date":{ 
    "epoch":"1411513200", 
    "pretty":"7:00 PM EDT on September 23, 2014", 
    "day":23, 
    "month":9, 
    "year":2014, 
    "yday":265, 
    "hour":19, 
    "min":"00", 
    "sec":0, 
    "isdst":"1", 
    "monthname":"September", 
    "monthname_short":"Sep", 
    "weekday_short":"Tue", 
    "weekday":"Tuesday", 
    "ampm":"PM", 
    "tz_short":"EDT", 
    "tz_long":"America/New_York" 
}, 
     "period":3, 
     "high": { 
     "fahrenheit":"72", 
     "celsius":"22" 
     }, 
     "low": { 
     "fahrenheit":"51", 
     "celsius":"11" 
     }, 
     "conditions":"Partly Cloudy", 
     "icon":"partlycloudy", 
     "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif", 
     "skyicon":"", 
     "pop":0, 
     "qpf_allday": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_day": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_night": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "snow_allday": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_day": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_night": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "maxwind": { 
     "mph": 10, 
     "kph": 16, 
     "dir": "NW", 
     "degrees": 321 
     }, 
     "avewind": { 
     "mph": 5, 
     "kph": 8, 
     "dir": "NW", 
     "degrees": 321 
     }, 
     "avehumidity": 52, 
     "maxhumidity": 0, 
     "minhumidity": 0 
     } 
     , 
     {"date":{ 
    "epoch":"1411599600", 
    "pretty":"7:00 PM EDT on September 24, 2014", 
    "day":24, 
    "month":9, 
    "year":2014, 
    "yday":266, 
    "hour":19, 
    "min":"00", 
    "sec":0, 
    "isdst":"1", 
    "monthname":"September", 
    "monthname_short":"Sep", 
    "weekday_short":"Wed", 
    "weekday":"Wednesday", 
    "ampm":"PM", 
    "tz_short":"EDT", 
    "tz_long":"America/New_York" 
}, 
     "period":4, 
     "high": { 
     "fahrenheit":"73", 
     "celsius":"23" 
     }, 
     "low": { 
     "fahrenheit":"54", 
     "celsius":"12" 
     }, 
     "conditions":"Clear", 
     "icon":"clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
     "skyicon":"", 
     "pop":10, 
     "qpf_allday": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_day": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_night": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "snow_allday": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_day": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_night": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "maxwind": { 
     "mph": 10, 
     "kph": 16, 
     "dir": "ENE", 
     "degrees": 66 
     }, 
     "avewind": { 
     "mph": 8, 
     "kph": 13, 
     "dir": "ENE", 
     "degrees": 66 
     }, 
     "avehumidity": 63, 
     "maxhumidity": 0, 
     "minhumidity": 0 
     } 
     , 
     {"date":{ 
    "epoch":"1411686000", 
    "pretty":"7:00 PM EDT on September 25, 2014", 
    "day":25, 
    "month":9, 
    "year":2014, 
    "yday":267, 
    "hour":19, 
    "min":"00", 
    "sec":0, 
    "isdst":"1", 
    "monthname":"September", 
    "monthname_short":"Sep", 
    "weekday_short":"Thu", 
    "weekday":"Thursday", 
    "ampm":"PM", 
    "tz_short":"EDT", 
    "tz_long":"America/New_York" 
}, 
     "period":5, 
     "high": { 
     "fahrenheit":"72", 
     "celsius":"22" 
     }, 
     "low": { 
     "fahrenheit":"52", 
     "celsius":"11" 
     }, 
     "conditions":"Partly Cloudy", 
     "icon":"partlycloudy", 
     "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif", 
     "skyicon":"", 
     "pop":20, 
     "qpf_allday": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_day": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_night": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "snow_allday": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_day": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_night": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "maxwind": { 
     "mph": 10, 
     "kph": 16, 
     "dir": "ENE", 
     "degrees": 63 
     }, 
     "avewind": { 
     "mph": 7, 
     "kph": 11, 
     "dir": "ENE", 
     "degrees": 63 
     }, 
     "avehumidity": 73, 
     "maxhumidity": 0, 
     "minhumidity": 0 
     } 
     , 
     {"date":{ 
    "epoch":"1411772400", 
    "pretty":"7:00 PM EDT on September 26, 2014", 
    "day":26, 
    "month":9, 
    "year":2014, 
    "yday":268, 
    "hour":19, 
    "min":"00", 
    "sec":0, 
    "isdst":"1", 
    "monthname":"September", 
    "monthname_short":"Sep", 
    "weekday_short":"Fri", 
    "weekday":"Friday", 
    "ampm":"PM", 
    "tz_short":"EDT", 
    "tz_long":"America/New_York" 
}, 
     "period":6, 
     "high": { 
     "fahrenheit":"78", 
     "celsius":"26" 
     }, 
     "low": { 
     "fahrenheit":"53", 
     "celsius":"12" 
     }, 
     "conditions":"Clear", 
     "icon":"clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
     "skyicon":"", 
     "pop":0, 
     "qpf_allday": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_day": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_night": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "snow_allday": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_day": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_night": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "maxwind": { 
     "mph": 5, 
     "kph": 8, 
     "dir": "NE", 
     "degrees": 37 
     }, 
     "avewind": { 
     "mph": 4, 
     "kph": 6, 
     "dir": "NE", 
     "degrees": 37 
     }, 
     "avehumidity": 63, 
     "maxhumidity": 0, 
     "minhumidity": 0 
     } 
     , 
     {"date":{ 
    "epoch":"1411858800", 
    "pretty":"7:00 PM EDT on September 27, 2014", 
    "day":27, 
    "month":9, 
    "year":2014, 
    "yday":269, 
    "hour":19, 
    "min":"00", 
    "sec":0, 
    "isdst":"1", 
    "monthname":"September", 
    "monthname_short":"Sep", 
    "weekday_short":"Sat", 
    "weekday":"Saturday", 
    "ampm":"PM", 
    "tz_short":"EDT", 
    "tz_long":"America/New_York" 
}, 
     "period":7, 
     "high": { 
     "fahrenheit":"81", 
     "celsius":"27" 
     }, 
     "low": { 
     "fahrenheit":"57", 
     "celsius":"14" 
     }, 
     "conditions":"Clear", 
     "icon":"clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
     "skyicon":"", 
     "pop":0, 
     "qpf_allday": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_day": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_night": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "snow_allday": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_day": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_night": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "maxwind": { 
     "mph": 5, 
     "kph": 8, 
     "dir": "N", 
     "degrees": 11 
     }, 
     "avewind": { 
     "mph": 4, 
     "kph": 6, 
     "dir": "N", 
     "degrees": 11 
     }, 
     "avehumidity": 58, 
     "maxhumidity": 0, 
     "minhumidity": 0 
     } 
     , 
     {"date":{ 
    "epoch":"1411945200", 
    "pretty":"7:00 PM EDT on September 28, 2014", 
    "day":28, 
    "month":9, 
    "year":2014, 
    "yday":270, 
    "hour":19, 
    "min":"00", 
    "sec":0, 
    "isdst":"1", 
    "monthname":"September", 
    "monthname_short":"Sep", 
    "weekday_short":"Sun", 
    "weekday":"Sunday", 
    "ampm":"PM", 
    "tz_short":"EDT", 
    "tz_long":"America/New_York" 
}, 
     "period":8, 
     "high": { 
     "fahrenheit":"80", 
     "celsius":"27" 
     }, 
     "low": { 
     "fahrenheit":"58", 
     "celsius":"14" 
     }, 
     "conditions":"Clear", 
     "icon":"clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
     "skyicon":"", 
     "pop":0, 
     "qpf_allday": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_day": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_night": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "snow_allday": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_day": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_night": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "maxwind": { 
     "mph": 10, 
     "kph": 16, 
     "dir": "E", 
     "degrees": 97 
     }, 
     "avewind": { 
     "mph": 6, 
     "kph": 10, 
     "dir": "E", 
     "degrees": 97 
     }, 
     "avehumidity": 64, 
     "maxhumidity": 0, 
     "minhumidity": 0 
     } 
     , 
     {"date":{ 
    "epoch":"1412031600", 
    "pretty":"7:00 PM EDT on September 29, 2014", 
    "day":29, 
    "month":9, 
    "year":2014, 
    "yday":271, 
    "hour":19, 
    "min":"00", 
    "sec":0, 
    "isdst":"1", 
    "monthname":"September", 
    "monthname_short":"Sep", 
    "weekday_short":"Mon", 
    "weekday":"Monday", 
    "ampm":"PM", 
    "tz_short":"EDT", 
    "tz_long":"America/New_York" 
}, 
     "period":9, 
     "high": { 
     "fahrenheit":"79", 
     "celsius":"26" 
     }, 
     "low": { 
     "fahrenheit":"61", 
     "celsius":"16" 
     }, 
     "conditions":"Partly Cloudy", 
     "icon":"partlycloudy", 
     "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif", 
     "skyicon":"", 
     "pop":20, 
     "qpf_allday": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_day": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "qpf_night": { 
     "in": 0.00, 
     "mm": 0 
     }, 
     "snow_allday": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_day": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_night": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "maxwind": { 
     "mph": 5, 
     "kph": 8, 
     "dir": "WSW", 
     "degrees": 240 
     }, 
     "avewind": { 
     "mph": 3, 
     "kph": 5, 
     "dir": "WSW", 
     "degrees": 240 
     }, 
     "avehumidity": 73, 
     "maxhumidity": 0, 
     "minhumidity": 0 
     } 
     , 
     {"date":{ 
    "epoch":"1412118000", 
    "pretty":"7:00 PM EDT on September 30, 2014", 
    "day":30, 
    "month":9, 
    "year":2014, 
    "yday":272, 
    "hour":19, 
    "min":"00", 
    "sec":0, 
    "isdst":"1", 
    "monthname":"September", 
    "monthname_short":"Sep", 
    "weekday_short":"Tue", 
    "weekday":"Tuesday", 
    "ampm":"PM", 
    "tz_short":"EDT", 
    "tz_long":"America/New_York" 
}, 
     "period":10, 
     "high": { 
     "fahrenheit":"73", 
     "celsius":"23" 
     }, 
     "low": { 
     "fahrenheit":"55", 
     "celsius":"13" 
     }, 
     "conditions":"Chance of Rain", 
     "icon":"chancerain", 
     "icon_url":"http://icons.wxug.com/i/c/k/chancerain.gif", 
     "skyicon":"", 
     "pop":60, 
     "qpf_allday": { 
     "in": 0.24, 
     "mm": 6 
     }, 
     "qpf_day": { 
     "in": 0.18, 
     "mm": 5 
     }, 
     "qpf_night": { 
     "in": 0.06, 
     "mm": 2 
     }, 
     "snow_allday": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_day": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "snow_night": { 
     "in": 0.0, 
     "cm": 0.0 
     }, 
     "maxwind": { 
     "mph": 10, 
     "kph": 16, 
     "dir": "NE", 
     "degrees": 50 
     }, 
     "avewind": { 
     "mph": 7, 
     "kph": 11, 
     "dir": "NE", 
     "degrees": 50 
     }, 
     "avehumidity": 81, 
     "maxhumidity": 0, 
     "minhumidity": 0 
     } 
     ] 
     } 
    } 
} 
+0

第一次发布了错误的json。用正确的json更新 – user2941841 2014-09-21 23:36:51

回答

1

尝试以下操作:

$forecasetp[] = array(); 
foreach ($parsed_json->forecast->txt_forecast->forecastday as $day) 
{ 
    $forecastp[] = $day->title; 
} 

这将f。添加title f作为$forecastp阵列的一个元素的值;你可以访问它作为$forecastp[0]

注:

  • 如果你只是想推的元素,没有他们设置一个特定的索引,使用$forecastp[]
  • PHP数组从0开始计数。

编辑:现在我明白你想要做什么。发布更新。

+0

是的,我从0开始。试图让它工作。更新的代码示例。仍然不能让它工作。 – user2941841 2014-09-21 23:22:42

+0

在根JSON对象上使用'foreach'而不是'for'。您不需要设置索引,只需使用我的示例中发布的“推送”即可。 – lxg 2014-09-21 23:24:14

+0

@ user2941841:啊,我想你想迭代'$ parsed_json-> forecast-> txt_forecast-> forecastday'列表。代码已更新。 – lxg 2014-09-21 23:26:59

0

由Ixg的答案可能会工作,但只有$ json_string是一个对象。

您也可以尝试

$forecastp[] = $parsed_json['forecast10day']['forecast']['txt_forecast']['forecastday'][0]->title; 

的基础是多数民众赞成 - >_是对象和[ '____']是数组。

+0

谢谢。我现在得到这个虽然:致命错误:不能使用stdClass类型的对象作为数组 – user2941841 2014-09-21 23:25:57

+0

你可以做print_r($ parsed_json)并将结果复制到这里吗? – 2014-09-21 23:27:38

+0

json正在解析发布 – user2941841 2014-09-21 23:33:22