0

I get some json data as follows:

$data = json_decode(file_get_contents("$api/$features$key$query"));
print_r2($data);

which gets me the following:

stdClass Object
(
    [location] => stdClass Object
        (
            [name] => Boston, Logan International Airport
            [region] => 
            [country] => United States
            [lat] => 42.36
            [lon] => -71.01
            [tz_id] => America/New_York
            [localtime_epoch] => 1554320436
            [localtime] => 2019-04-03 15:40
        )

    [current] => stdClass Object
        (
            [condition] => stdClass Object
                (
                )

            [uv] => 5
        )

    [forecast] => stdClass Object
        (
            [forecastday] => Array
                (
                    [0] => stdClass Object
                        (
                            [day] => stdClass Object
                                (
                                    [condition] => stdClass Object
                                        (
                                        )

                                )

                            [astro] => stdClass Object
                                (
                                )

                            [hour] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [time] => 2019-04-03 00:00
                                            [temp_f] => 41
                                            [condition] => stdClass Object
                                                (
                                                    [text] => Moderate rain
                                                )

                                            [wind_mph] => 9.4
                                            [wind_degree] => 91
                                            [wind_dir] => E
                                            [pressure_in] => 30.7
                                            [precip_mm] => 1
                                            [humidity] => 75
                                            [cloud] => 94
                                            [feelslike_f] => 35.1
                                            [windchill_f] => 35.1
                                            [heatindex_f] => 41
                                            [dewpoint_f] => 33.4
                                            [will_it_rain] => 0

When I try to get the location information I use:

$location = $data['location'];

but this doesn't get me the data, I also tried = $data['location']['name'] but still no data

DCR
  • 14,737
  • 12
  • 52
  • 115
  • 3
    Add `true` to the `json_decode()` statement to convert the JSON to an array, then you can get the data using the syntax you're using. What does `var_dump($data->location)` get you? – Jay Blanchard Apr 03 '19 at 19:49
  • 1
    Voting typo even if it's not really a typo. But still it's to little of a problem for it to be a question to answer – Andreas Apr 03 '19 at 19:51
  • awesome, that's all it was. thanks – DCR Apr 03 '19 at 19:57

1 Answers1

2

Use second parameter true

$data = json_decode(file_get_contents("$api/$features$key$query"),true);

This will convert your data to asociative array and then you can manipulate with it like with an asociative array.

The way you are converting it, is an object and thats why you would have to treat it as an object.
Instead of calling

$location = $data['location'];

you would have to have

$location = $data->location;
matri70boss
  • 349
  • 2
  • 13