0

I'm trying to retrieve the first two ("period": 0 and "period": 1) icon_url values from the json file showed at the bottom.

However, I'm stuck at:

for($j=0; $j<2; $j++) {
$icon[$j] = $parsed_json_forecast->{'forecast'}->{'txt_forecast'}->{'forecastday'}->

How to parse the first two icon_url values?

Of course it can't be:

[...]->{'period'}->{$j}->{'icon_url'};

Here's the json content:

{
    "response": {
        "version": "0.1",
        "termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
        "features": {
            "forecast": 1
        }
    },
    "forecast": {
        "txt_forecast": {
            "date": "2:00 AM CEST",
            "forecastday": [{
                "period": 0,
                "icon": "rain",
                "icon_url": "http://icons-ak.wxug.com/i/c/k/rain.gif",
                "title": "Martedì",
                "fcttext": "Pioggia. Massima: 77F. Velocità del vento: 15-25 mph. Direzione del vento: Ovest. Possibilità di precip. 90%.",
                "fcttext_metric": "Pioggia. Massima: 25C. Velocità del vento: 30-40 km/h. Direzione del vento: Ovest. Possibilità di precip. 90%.",
                "pop": "90"
            },
            {
                "period": 1,
                "icon": "partlycloudy",
                "icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
                "title": "Martedì notte",
                "fcttext": "Pioggia. Minima: 63F. Velocità del vento: 5-15 mph. Direzione del vento: Ovest-NE.",
                "fcttext_metric": "Pioggia. Minima: 17C. Velocità del vento: 5-20 km/h. Direzione del vento: Ovest-NE.",
                "pop": "0"
            },
            {
                "period": 2,
                "icon": "clear",
                "icon_url": "http://icons-ak.wxug.com/i/c/k/clear.gif",
                "title": "Mercoledì",
                "fcttext": "Sereno. Massima: 77F. Velocità del vento: 5-10 mph. Direzione del vento: Sud.",
                "fcttext_metric": "Sereno. Massima: 25C. Velocità del vento: 10-15 km/h. Direzione del vento: Sud.",
                "pop": "0"
            },
            {
                "period": 3,
                "icon": "clear",
                "icon_url": "http://icons-ak.wxug.com/i/c/k/clear.gif",
                "title": "Mercoledì notte",
                "fcttext": "Sereno. Minima: 64F. Vento leggero.",
                "fcttext_metric": "Sereno. Minima: 18C. Vento leggero.",
                "pop": "20"
            },      
[....]

Thanks in advance.

3
  • $parsed_json->forecast->txt_forecast->forecastday[$j]->icon_url. You are indexing into an array, use array syntax. What is confusing about it? Commented Sep 17, 2013 at 10:45
  • That "period" value somewhat confused me (I'm still unexperienced with arrays). Of course you're right. Commented Sep 17, 2013 at 10:59
  • Thanks to anybody to answered. Sometimes it's difficult to choose just an answer as "the right one", I hope you'll understand it. Commented Sep 17, 2013 at 11:01

3 Answers 3

2

When you have parsed the JSON data (e.g with json_decode) you may access each value using the regular php array syntax:

$json["forecast"]["txt_forecast"]["forecastday"][0]["period"].

Edit: It's the same for array access (SPL):

$json->forecast->txt_forecast->forecastday[0]->icon_url
Sign up to request clarification or add additional context in comments.

6 Comments

That doesn't answer the question.
@AmalMurali How does it not? Parsing json without decoding would be very resource heavy if you are thinking of preg_match or like.
No. The OP is using objects, but you're using associative arrays in your answer instead.
Only if you pass true as second argument to json_decode, objects are parsed into associative arrays.
Yes, but you can only use array syntax to access JSON data encoded as objects if you pass true as second argument and you don't mention that. If the OP just uses json_decode, they won't be able to use your solution.
|
1

You have the JSON string. Now, to extract the required values, you'll have to do:

  • decode the the JSON using json_decode()
  • loop through the required items
  • store them in your array

Code:

$parsed_json = json_decode($str);

for($j=0; $j<2; $j++) {
    $icon[$j] = $parsed_json->forecast->txt_forecast->forecastday[$j]->icon_url;
}

Output:

Array
(
    [0] => http://icons-ak.wxug.com/i/c/k/rain.gif
    [1] => http://icons-ak.wxug.com/i/c/k/partlycloudy.gif
)

Demo!

Comments

1

First, you need to parse your json data using json_decode and you can able to extract the data,

Here's the sample code,

$data = json_decode($json);
for($j=0; $j<2; $j++) {
    echo $data->forecast->txt_forecast->forecastday[$j]->icon_url;
}

Code Demo: http://codepad.org/O9WsTs34

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.