0

I am doing something wrong but I cannot figure it out.

I have this JSON

$data = {  
   "data":[  
      {  
         "app_min_temp":15.5,
         "valid_date":"2018-08-10",
         "weather":{  
            "icon":"c02d",
            "code":801,
            "description":"Few clouds"
         },
         "max_temp":26.3,
         "datetime":"2018-08-11"
      },
      {  
         "app_min_temp":18.5,
         "valid_date":"2018-08-11",
         "weather":{  
            "icon":"c02d",
            "code":801,
            "description":"Few clouds"
         },
         "max_temp":26.3,
         "datetime":"2018-08-11"
      }
   ],
   "city_name":"Berlin"
};

Expected Result And trying to get the values from app_min_temp and valid_date and print them all together. Something like

app_min_temp:15.5 valid_date: 2018-08-10 Weather description: Few clouds
app_min_temp:18.5 valid_date: 2018-08-11 Weather description: Few clouds

FOREACH I am trying to foreach trougn the json like this

$json = json_decode($data);
foreach($json as $data) {
        echo "app_min_temp: ".$data[0]->app_min_temp.PHP_EOL."valid_date: ".$data[0]->valid_date.PHP_EOL."Weather description: ".$data[0]->weather->description.PHP_EOL; //etc
    }

The Results But I am getting strange result with only the first set of data printed. Like:

app_min_temp: 15.5
valid_date: 2018-08-10
Weather description: Few clouds

app_min_temp: 
valid_date: 
Weather description: 
app_min_temp: 
valid_date: 
Weather description: 
app_min_temp: 
valid_date: 
Weather description: 
app_min_temp: 
valid_date: 
Weather description: 
app_min_temp: 
valid_date: 
Weather description: 
app_min_temp: 
valid_date: 
Weather description: 

Thanks

3 Answers 3

2

There is a data key in your JSON object you need to access first. Also, the array index access is done by foreach already.

foreach ($json->data as $row) {
   echo "app_min_temp: ".$row->app_min_temp;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I knew that I am missing something general. Just couldn't spot it
1

Another solution based on this post where it says that "if you want an associative array instead of an object from json_decode" you should do this way:

$json = json_decode($data,true);  // CHANGE

foreach($json['data'] as $data2) {
        echo "app_min_temp: ".$data2['app_min_temp'].PHP_EOL.
            "valid_date: ".$data2['valid_date'].PHP_EOL.
            "Weather description: ".$data2['weather']['description'].PHP_EOL; //etc
}

The true flag means returned objects will be converted into associative arrays.

Comments

1

You missed the nesting in JSON data. Your $data and json's data are totally different. I changed your loop section a bit . And the output was as you expected.

 <?php
$data = '{  
  "data":[  
   {  
     "app_min_temp":15.5,
     "valid_date":"2018-08-10",
     "weather":{  
        "icon":"c02d",
        "code":801,
        "description":"Few clouds"
     },
     "max_temp":26.3,
     "datetime":"2018-08-11"
  },
  {  
     "app_min_temp":18.5,
     "valid_date":"2018-08-11",
     "weather":{  
        "icon":"c02d",
        "code":801,
        "description":"Few clouds"
     },
     "max_temp":26.3,
     "datetime":"2018-08-11"
  }
],
"city_name":"Berlin"
}';

$json = json_decode($data);

foreach($json->data as $data) {
    echo "app_min_temp: ".$data->app_min_temp.PHP_EOL."valid_date: ".$data-   >valid_date.PHP_EOL."Weather description: ".$data->weather->description."<br>"; 
}
?>

Output

app_min_temp: 15.5 valid_date: 2018-08-10 Weather description: Few clouds
app_min_temp: 18.5 valid_date: 2018-08-11 Weather description: Few clouds

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.