K
K
Kostya Tester2018-08-01 15:55:49
PHP
Kostya Tester, 2018-08-01 15:55:49

Can't output a specific string in json?

{"cod":"200","message":0.0021,"cnt":1,"list":[{"dt":1533135600,"main":{"temp":27.88,"temp_min":26.74,"temp_max":27.88,"pressure":1017.51,"sea_level":1036.77,"grnd_level":1017.51,"humidity":53,"temp_kf":1.13},"weather":[{"id":800,"main":"Clear","description":"ясно","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":1.97,"deg":19.0015},"sys":{"pod":"d"},"dt_txt":"2018-08-01 15:00:00"}],"city":{"id":524901,"name":"Moscow","coord":{"lat":55.7507,"lon":37.6177},"country":"RU","population":1000000}}

Connecting to json via php. I do the output through php foreach, everything is displayed except for the weather line. Can anyone give me a hint???
Sample output
$data = json_decode(file_get_contents('json file'));
<?php foreach($data->list as $list): ?>
<?= $list->main->temp ?> Value
printed by <?php endforeach; ?>
I did something like this, but it doesn't output anything (although I think I did it right)
<?php foreach($data->list->weather as $list): ?>
<?= $list->icon ?> Value doesn't output
< ?phpendforeach; ?>
Help urgently needed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BI0R0B0T, 2018-08-01
@dkostia

Weather you have an array. You missed the index call. This is how it outputs:

<?php
$data = json_decode('{"cod":"200","message":0.0021,"cnt":1,"list":[{"dt":1533135600,"main":{"temp":27.88,"temp_min":26.74,"temp_max":27.88,"pressure":1017.51,"sea_level":1036.77,"grnd_level":1017.51,"humidity":53,"temp_kf":1.13},"weather":[{"id":800,"main":"Clear","description":"ясно","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":1.97,"deg":19.0015},"sys":{"pod":"d"},"dt_txt":"2018-08-01 15:00:00"}],"city":{"id":524901,"name":"Moscow","coord":{"lat":55.7507,"lon":37.6177},"country":"RU","population":1000000}}');
?>
<?php foreach($data->list as $list): ?>
<?= $list->main->temp ?> Значение выводит
<?php foreach($list->weather as $l): ?>
<?= $l->icon ?> Значение невыводит
<?php endforeach; ?>
<?php endforeach; ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question