There is an array of JSON:

{"city":{"id":2013159,"name":"London", "list": [ {"dt":1459360800,"main":{"temp":-6.55,"temp_min":-6.55,"temp_max":-2.34},"weather":[{"id":800,"main":"Clear","description":"ясно"}], {"dt":1459425600,"main":{"temp":-8.55,"temp_min":-10.55,"temp_max":-4.34},"weather":[{"id":700,"main":"Cloud","description":"Облака"}], {"dt":1459458000,"main":{"temp":-10.56,"temp_min":-15.55,"temp_max":-8.34},"weather":[{"id":600,"main":"Clear","description":"ясно"}] ]} 

it is in the variable $ weatherDatas_json Converted JSON data into an array variable

 $weatherDatas = json_decode($weatherDatas_json, true); 

To display the data, all dt do this:

 foreach ($weatherDatas["list"] as $key => $value) { echo $value["dt"],"\n"; } 

Please help me with the output syntax: I want to output dt data from the first list array, and I can’t get the data from the second array, for example, id , like this:

 foreach ($weatherDatas["list"]["weather"] as $key => $value) { echo $value["dt"],$value["id"],"\n"; } 

but this does not work, error: PHP Notice: Undefined index: weather

    1 answer 1

    By line

     $weatherDatas["list"]["weather"] 

    You are trying to access the element with id => weather in the list array. An element with such an id is not there.

    Look again at the structure of what you have:

    • list is an array of objects.
    • Each such object has the properties dt, main, weather.
    • weather, in turn, is an array of objects.
    • Each object in the weather array has a Holy Name id, main, description.

    Total we get:

     foreach ($weatherDatas["list"] as $value) { foreach($value["weather"] as $weather) { echo $weather["id"],"\n"; } } 

    Those. for each element in the list, we output the id of each weather element

    • Thank! all very lucid - mocart