Do Thanh Dat Do Thanh Dat - 7 months ago 8
PHP Question

Get all array keys that has same value (date) PHP Openweathermap

I have

{"city":{"id":5128581,"name":"New York","coord":{"lon":-74.005966,"lat":40.714272},"country":"US","population":0,"sys":{"population":0}},"cod":"200","message":0.0157,"cnt":34,"list":[{"dt":1462730400,"main":{"temp":16.29,"temp_min":15.26,"temp_max":16.29,"pressure":1013.67,"sea_level":1016.94,"grnd_level":1013.67,"humidity":76,"temp_kf":1.03},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":0},"wind":{"speed":6.02,"deg":285.502},"rain":{"3h":0.005},"sys":{"pod":"d"},"dt_txt":"2016-05-08 18:00:00"},{"dt":1462741200,"main":{"temp":16.27,"temp_min":15.58,"temp_max":16.27,"pressure":1016.17,"sea_level":1019.42,"grnd_level":1016.17,"humidity":49,"temp_kf":0.69},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":6.3,"deg":299.501},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-08 21:00:00"},{"dt":1462752000,"main":{"temp":14.75,"temp_min":14.4,"temp_max":14.75,"pressure":1019.5,"sea_level":1022.93,"grnd_level":1019.5,"humidity":48,"temp_kf":0.34},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":56},"wind":{"speed":5.35,"deg":281.001},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-09 00:00:00"},{"dt":1462762800,"main":{"temp":13.13,"temp_min":13.13,"temp_max":13.13,"pressure":1022.28,"sea_level":1025.77,"grnd_level":1022.28,"humidity":52,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":4.76,"deg":277.503},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-09 03:00:00"},{"dt":1462773600,"main":{"temp":11.86,"temp_min":11.86,"temp_max":11.86,"pressure":1023.68,"sea_level":1027.06,"grnd_level":1023.68,"humidity":56,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":4.07,"deg":276.501},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-09 06:00:00"},{"dt":1462784400,"main":{"temp":10.11,"temp_min":10.11,"temp_max":10.11,"pressure":1024.81,"sea_level":1028.24,"grnd_level":1024.81,"humidity":66,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":4.01,"deg":248.007},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-09 09:00:00"},{"dt":1462795200,"main":{"temp":12.37,"temp_min":12.37,"temp_max":12.37,"pressure":1025.97,"sea_level":1029.33,"grnd_level":1025.97,"humidity":58,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":3.56,"deg":244.001},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-09 12:00:00"},{"dt":1462806000,"main":{"temp":17.12,"temp_min":17.12,"temp_max":17.12,"pressure":1025.94,"sea_level":1029.3,"grnd_level":1025.94,"humidity":47,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":5.28,"deg":254.505},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-09 15:00:00"},{"dt":1462816800,"main":{"temp":19.47,"temp_min":19.47,"temp_max":19.47,"pressure":1025.22,"sea_level":1028.61,"grnd_level":1025.22,"humidity":40,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":5.7,"deg":267.503},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-09 18:00:00"},{"dt":1462827600,"main":{"temp":20.43,"temp_min":20.43,"temp_max":20.43,"pressure":1024.61,"sea_level":1028.02,"grnd_level":1024.61,"humidity":36,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":5.41,"deg":268.506},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-09 21:00:00"},{"dt":1462838400,"main":{"temp":18.92,"temp_min":18.92,"temp_max":18.92,"pressure":1025.5,"sea_level":1028.87,"grnd_level":1025.5,"humidity":37,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":4.71,"deg":267},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-10 00:00:00"},{"dt":1462849200,"main":{"temp":15.24,"temp_min":15.24,"temp_max":15.24,"pressure":1027.67,"sea_level":1031.12,"grnd_level":1027.67,"humidity":45,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":3.01,"deg":320.504},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-10 03:00:00"},{"dt":1462860000,"main":{"temp":12.42,"temp_min":12.42,"temp_max":12.42,"pressure":1029.87,"sea_level":1033.33,"grnd_level":1029.87,"humidity":50,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":2.87,"deg":0.5},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-10 06:00:00"},{"dt":1462870800,"main":{"temp":8.48,"temp_min":8.48,"temp_max":8.48,"pressure":1031.42,"sea_level":1034.85,"grnd_level":1031.42,"humidity":64,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"02n"}],"clouds":{"all":8},"wind":{"speed":2.21,"deg":19.5059},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-10 09:00:00"},{"dt":1462881600,"main":{"temp":12,"temp_min":12,"temp_max":12,"pressure":1032.33,"sea_level":1035.75,"grnd_level":1032.33,"humidity":53,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":1.3,"deg":46.001},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-10 12:00:00"},{"dt":1462892400,"main":{"temp":15.65,"temp_min":15.65,"temp_max":15.65,"pressure":1032.61,"sea_level":1036.02,"grnd_level":1032.61,"humidity":44,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":20},"wind":{"speed":1.46,"deg":178.503},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-10 15:00:00"},{"dt":1462903200,"main":{"temp":15.97,"temp_min":15.97,"temp_max":15.97,"pressure":1031.52,"sea_level":1034.7,"grnd_level":1031.52,"humidity":44,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":76},"wind":{"speed":3.12,"deg":221.001},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-10 18:00:00"},{"dt":1462914000,"main":{"temp":15.47,"temp_min":15.47,"temp_max":15.47,"pressure":1030,"sea_level":1033.18,"grnd_level":1030,"humidity":48,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":92},"wind":{"speed":3.39,"deg":220.502},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-10 21:00:00"},{"dt":1462924800,"main":{"temp":14.45,"temp_min":14.45,"temp_max":14.45,"pressure":1029.57,"sea_level":1032.9,"grnd_level":1029.57,"humidity":55,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":92},"wind":{"speed":3.42,"deg":223.001},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-11 00:00:00"},{"dt":1462935600,"main":{"temp":14.05,"temp_min":14.05,"temp_max":14.05,"pressure":1029.6,"sea_level":1032.97,"grnd_level":1029.6,"humidity":64,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":92},"wind":{"speed":2.61,"deg":233.504},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-11 03:00:00"},{"dt":1462946400,"main":{"temp":12.98,"temp_min":12.98,"temp_max":12.98,"pressure":1029.21,"sea_level":1032.63,"grnd_level":1029.21,"humidity":72,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":92},"wind":{"speed":2.15,"deg":254.501},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-11 06:00:00"},{"dt":1462957200,"main":{"temp":11.62,"temp_min":11.62,"temp_max":11.62,"pressure":1029.25,"sea_level":1032.71,"grnd_level":1029.25,"humidity":80,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":92},"wind":{"speed":0.98,"deg":273.501},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-11 09:00:00"},{"dt":1462968000,"main":{"temp":13.73,"temp_min":13.73,"temp_max":13.73,"pressure":1030.19,"sea_level":1033.67,"grnd_level":1030.19,"humidity":65,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":92},"wind":{"speed":1.21,"deg":352},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-11 12:00:00"},{"dt":1462978800,"main":{"temp":16.82,"temp_min":16.82,"temp_max":16.82,"pressure":1030.72,"sea_level":1034.15,"grnd_level":1030.72,"humidity":45,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":92},"wind":{"speed":1.56,"deg":63.0032},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-11 15:00:00"},{"dt":1462989600,"main":{"temp":18.75,"temp_min":18.75,"temp_max":18.75,"pressure":1029.77,"sea_level":1033.18,"grnd_level":1029.77,"humidity":42,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":1.56,"deg":114.005},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-11 18:00:00"},{"dt":1463000400,"main":{"temp":18.92,"temp_min":18.92,"temp_max":18.92,"pressure":1028.57,"sea_level":1032,"grnd_level":1028.57,"humidity":40,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":20},"wind":{"speed":1.76,"deg":158.501},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-11 21:00:00"},{"dt":1463011200,"main":{"temp":15.8,"temp_min":15.8,"temp_max":15.8,"pressure":1029.06,"sea_level":1032.42,"grnd_level":1029.06,"humidity":47,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"clouds":{"all":20},"wind":{"speed":2.32,"deg":171.5},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-12 00:00:00"},{"dt":1463022000,"main":{"temp":10.9,"temp_min":10.9,"temp_max":10.9,"pressure":1030.34,"sea_level":1033.69,"grnd_level":1030.34,"humidity":71,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":36},"wind":{"speed":1.16,"deg":156},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-12 03:00:00"},{"dt":1463032800,"main":{"temp":10.16,"temp_min":10.16,"temp_max":10.16,"pressure":1030.41,"sea_level":1033.88,"grnd_level":1030.41,"humidity":81,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":80},"wind":{"speed":1.06,"deg":88.5057},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-12 06:00:00"},{"dt":1463043600,"main":{"temp":10.2,"temp_min":10.2,"temp_max":10.2,"pressure":1030.3,"sea_level":1033.76,"grnd_level":1030.3,"humidity":82,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":76},"wind":{"speed":1.4,"deg":55.501},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-12 09:00:00"},{"dt":1463054400,"main":{"temp":12.33,"temp_min":12.33,"temp_max":12.33,"pressure":1030.4,"sea_level":1033.92,"grnd_level":1030.4,"humidity":72,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":76},"wind":{"speed":2.02,"deg":57.5028},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-12 12:00:00"},{"dt":1463065200,"main":{"temp":16.46,"temp_min":16.46,"temp_max":16.46,"pressure":1030.83,"sea_level":1034.2,"grnd_level":1030.83,"humidity":53,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":48},"wind":{"speed":2.32,"deg":75.5036},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-12 15:00:00"},{"dt":1463076000,"main":{"temp":18.67,"temp_min":18.67,"temp_max":18.67,"pressure":1030.02,"sea_level":1033.43,"grnd_level":1030.02,"humidity":43,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":36},"wind":{"speed":2.56,"deg":110.51},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-12 18:00:00"},{"dt":1463086800,"main":{"temp":17.58,"temp_min":17.58,"temp_max":17.58,"pressure":1028.71,"sea_level":1032.23,"grnd_level":1028.71,"humidity":44,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":92},"wind":{"speed":2.77,"deg":115.504},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-12 21:00:00"}]}


And here's my code

$data = file_get_contents('http://api.openweathermap.org/data/2.5/forecast?q=HauGiang,vi&appid=307f2a5a7c6940057fcde7ccbb47c623&units=metric');
$json = json_decode($data);
foreach ($json->list as $myday){
echo "Time: ".date($myday->dt_txt);
echo '</br>';
}


Desire Output:

enter image description here

I want to get all same value has same day (without time) to insert into div, so how can i do that? Sorry for my english . Thank you so much !

Answer
$json = json_decode($data);
$arr = array();
foreach ($json->list as $myday){
    $date = date("d", strtotime($myday->dt_txt));
    $arr[$date][] = date("Y-m-d H:00:00", strtotime($myday->dt_txt));       
}

foreach($arr as $value){
    echo '<div>';
    foreach($value as $val){
        echo 'Time:'. $val;
    }
    echo '</div><hr/>';
}

Result:

Time:2016-05-08 18:00:00
Time:2016-05-08 21:00:00
------------------------
Time:2016-05-09 00:00:00
Time:2016-05-09 03:00:00
Time:2016-05-09 06:00:00
Time:2016-05-09 09:00:00
Time:2016-05-09 12:00:00
Time:2016-05-09 15:00:00
Time:2016-05-09 18:00:00
Time:2016-05-09 21:00:00
------------------------
Time:2016-05-10 00:00:00
Time:2016-05-10 03:00:00
Time:2016-05-10 06:00:00
Time:2016-05-10 09:00:00
Time:2016-05-10 12:00:00
Time:2016-05-10 15:00:00
Time:2016-05-10 18:00:00
Time:2016-05-10 21:00:00
------------------------
Time:2016-05-11 00:00:00
Time:2016-05-11 03:00:00
Time:2016-05-11 06:00:00
Time:2016-05-11 09:00:00
Time:2016-05-11 12:00:00
Time:2016-05-11 15:00:00
Time:2016-05-11 18:00:00
Time:2016-05-11 21:00:00
------------------------    
Time:2016-05-12 00:00:00
Time:2016-05-12 03:00:00
Time:2016-05-12 06:00:00
Time:2016-05-12 09:00:00
Time:2016-05-12 12:00:00
Time:2016-05-12 15:00:00
Time:2016-05-12 18:00:00
Time:2016-05-12 21:00:00