0

I just need a little help with json and php. How do i echo certian parts of data if my request comes back looking like this:

{ "data": { "current_condition": [ {"cloudcover": "2", "humidity": "54", "observation_time": "09:05 PM", "precipMM": "0.0", "pressure": "1019", "temp_C": "11", "visibility": "10", "weatherCode": "113",  "weatherDesc": [ {"value": "Clear" } ],  "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0008_clear_sky_night.png" } ], "winddir16Point": "N", "winddirDegree": "350", "windspeedKmph": "15", "windspeedMiles": "9" } ],  "request": [ {"query": "48.85,2.35", "type": "LatLon" } ] }}

I am using a weather API, my code currently looks like:

$weather_url = file_get_contents("http://free.worldweatheronline.com/feed/weather.ashx?q=xxxxx&format=json&num_of_days=2&key=xxxxxxxxxxxxxxxxxx");
$json_output_w = json_decode($weather_url, true);

the q in the url string can be a zipcode, lat and long, or a city, i know it is returning data because i can dump the variable $json_output_w; but i just need a little guidance as how to actually echo certain parts of the data that is returned. like say i wanted to echo windspeedMiles

3
  • 1
    var_dump($json_output_w); ? Commented Jun 29, 2011 at 0:48
  • i don't want to display all of the data, i am just looking for some helo with displaying certain information that in that array of data, like echo $json_output_w['data']['current_condition']['cloudcover']; but that doesn't do anything for me. Commented Jun 29, 2011 at 0:50
  • if that doesn't return anything - then either there is nothing in that item or you're trying to retrieve a key that doesn't exist. So you still need var_dump() to inspect your array. Commented Jun 29, 2011 at 0:55

1 Answer 1

3

The json_decode() function will return either an object or array (depending on the second argument). You can explore the structure of the returned item using the var_dump() function:

var_dump( $json_output_w );

From here you would discover what type of structure you need to consider when pulling values out. To get the windSpeedMiles value, you'd do the following:

echo $json_output_w["data"]["current_condition"][0]["windspeedMiles"];

Online Demo: http://codepad.org/BfhHbQMz

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.