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}} {"dt":1459425600,"main":{"temp":-8.55,"temp_min":-10.55,"temp_max":-4.34}} {"dt":1459458000,"main":{"temp":-10.56,"temp_min":-15.55,"temp_max":-8.34}} ]} 

it is in the $ weatherDatas_json variable
Convert JSON data to an array variable

 $weatherDatas = json_decode($weatherDatas_json, true); 

To output dt data from a 1 ( zero ) array element, do the following:

 $weatherDt = $weatherDatas['city']['list'][0][dt]; echo $weatherDt; 

How to remove all dt from an array? Please tell me the while construct. I can not substitute the element number of the array automatically, and the data dt (array elements) can be a different number

  • and while? maybe this will do: foreach ($array as $key => $value) { echo "Index: $key, value: $value"; } foreach ($array as $key => $value) { echo "Index: $key, value: $value"; } ? Through while, you can, for example, like this: while ($value = array_pop($array)) { echo $value; } while ($value = array_pop($array)) { echo $value; } There are a lot of ways, depending on the task, some may not work, but everything should work for output - BOPOH

2 answers 2

why do you need while? There is definitely foreach (while - slower in order just) How to limit the number of output? you start the $ limit counter, ply it when necessary, and check at the beginning of the cycle:

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

    https://jonsuh.com/blog/convert-loop-through-json-php-javascript-arrays-objects/ - described here very well

    It was done as follows: first of all, for a visual understanding of the array, it is better to output it through the operator:

     print_r($weatherDatas); 

    further, to select all elements from the array, use the operator:

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

    the only thing is how to limit the number of output elements , for example, to display the first 5 elements of the array?

    • If you need a restriction, it is customary to start a loop with a counter. for($i = 0; $i < 5; $i++) { $curItem = $weatherDatas['list'][$i]; var_dump($curItem;); } for($i = 0; $i < 5; $i++) { $curItem = $weatherDatas['list'][$i]; var_dump($curItem;); } - user200141
    • Well, $ key you for what? - Ipatiev
    • @ Ipatiev: Where did you find the $key ? The key's role, as you can see, is enclosed in $i - user200141