Error picking up a json element with php

Asked

Viewed 64 times

1

I have the following result in json:

Array
(
    [0] => Array
        (
            [LocalObservationDateTime] => 2019-11-12T19:20:00-03:00
            [EpochTime] => 1573597200
            [WeatherText] => Algumas nuvens
            [WeatherIcon] => 36
            [HasPrecipitation] => 
            [PrecipitationType] => 
            [IsDayTime] => 
            [Temperature] => Array
                (
                    [Metric] => Array
                        (
                            [Value] => 28.7
                            [Unit] => C
                            [UnitType] => 17
                        )

                    [Imperial] => Array
                        (
                            [Value] => 84
                            [Unit] => F
                            [UnitType] => 18
                        )

                )

            [RealFeelTemperature] => Array
                (
                    [Metric] => Array
                        (
                            [Value] => 32.7
                            [Unit] => C
                            [UnitType] => 17
                        )

                    [Imperial] => Array
                        (
                            [Value] => 91
                            [Unit] => F
                            [UnitType] => 18
                        )   
                )
[...]

I’m trying to catch up with PHP this way:

<?php
$detalhes = $data["WeatherText"];
//também tentei desta forma abaixo:
$detalhes = $data->WeatherText;
?>

I make a var_dump($detalhes), returns NULL

Where I’m going wrong?

1 answer

2


You’re giving trouble because your object is a array the first element of which is a array.

An example with a structure object similar to yours:

<?php

//Aqui o JSON é um array cujo o dois elementos são objetos
$json = '[ {"a":1,"b":2,"c":3,"d":4,"e":5}, {"f":10,"g":20,"h":30,"i":40,"j":50} ]';

//Converte o JSON em array associativo
$arr = json_decode($json, true);

//Imprime a estrutura do array associativo
print_r($arr);

//Imprime o valor da chave 'c' do primeiro elemento
print($arr[0]["c"]);

?>

In doing print_r($arr); the result is JSON structure as an associative array:

Array
(
    [0] => Array
        (
            [a] => 1
            [b] => 2
            [c] => 3
            [d] => 4
            [e] => 5
        )

    [1] => Array
        (
            [f] => 10
            [g] => 20
            [h] => 30
            [i] => 40
            [j] => 50
        )

)

And in doing print($arr[0]["c"]); the result is :

3

Then go back to your example to get the desired value refer to the element you want to access followed by the key you want to get the value:

$detalhes = $data[0]["WeatherText"];

Link to code on Repl.it

  • Thank you for the reply. At the moment the API I am using has exceeded the daily request limit. Tomorrow I’ll try again and come back here with the result.

  • @Leo, OK, I’m waiting for the result.

  • 1

    worked out, grateful!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.