Amitsd Amitsd - 5 months ago 11
PHP Question

Getting data from complex nested JSON object (using PHP)

I am trying to just get the minimum and maximum temperature for time "15:00:00" from this complex nested JSON data. Can anyone please assist me on how to extract the data?

The JSON data looks like this:

{
"city": {
"id": 1609350,
"name": "Bangkok",
"coord": {
"lon": 100.51667,
"lat": 13.75
},
"country": "TH",
"population": 0,
"sys": {
"population": 0
}
},
"cod": "200",
"message": 0.0169,
"cnt": 38,
"list": [{
"dt": 1466402400,
"main": {
"temp": 311.79,
"temp_min": 308.315,
"temp_max": 311.79,
"pressure": 1018.37,
"sea_level": 1018.77,
"grnd_level": 1018.37,
"humidity": 61,
"temp_kf": 3.47
},
"weather": [{
"id": 801,
"main": "Clouds",
"description": "few clouds",
"icon": "02d"
}],
"clouds": {
"all": 24
},
"wind": {
"speed": 2.14,
"deg": 269.001
},
"sys": {
"pod": "d"
},
"dt_txt": "2016-06-20 06:00:00"
}, {
"dt": 1466413200,
"main": {
"temp": 309.22,
"temp_min": 306.611,
"temp_max": 309.22,
"pressure": 1016.83,
"sea_level": 1017.28,
"grnd_level": 1016.83,
"humidity": 61,
"temp_kf": 2.6
},
"weather": [{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}],
"clouds": {
"all": 36
},
"wind": {
"speed": 1.07,
"deg": 132.503
},
"rain": {
"3h": 1.88
},
"sys": {
"pod": "d"
},
"dt_txt": "2016-06-20 09:00:00"
}, {
"dt": 1466424000,
"main": {
"temp": 307.17,
"temp_min": 305.429,
"temp_max": 307.17,
"pressure": 1017.62,
"sea_level": 1018.09,
"grnd_level": 1017.62,
"humidity": 59,
"temp_kf": 1.74
},
"weather": [{
"id": 804,
"main": "Clouds",
"description": "overcast clouds",
"icon": "04n"
}],
"clouds": {
"all": 92
},
"wind": {
"speed": 1.33,
"deg": 153.501
},
"rain": {},
"sys": {
"pod": "n"
},
"dt_txt": "2016-06-20 12:00:00"
}, {
"dt": 1466434800,
"main": {
"temp": 303.96,
"temp_min": 303.092,
"temp_max": 303.96,
"pressure": 1019.69,
"sea_level": 1020.02,
"grnd_level": 1019.69,
"humidity": 66,
"temp_kf": 0.87
},
"weather": [{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04n"
}],
"clouds": {
"all": 76
},
"wind": {
"speed": 4.26,
"deg": 180.504
},
"rain": {},
"sys": {
"pod": "n"
},
"dt_txt": "2016-06-20 15:00:00"
}, {
"dt": 1466445600,
"main": {
"temp": 301.672,
"temp_min": 301.672,
"temp_max": 301.672,
"pressure": 1019.5,
"sea_level": 1019.9,
"grnd_level": 1019.5,
"humidity": 74,
"temp_kf": 0
},
"weather": [{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "03n"
}],
"clouds": {
"all": 48
},
"wind": {
"speed": 3.06,
"deg": 163.501
},
"rain": {},
"sys": {
"pod": "n"
},
"dt_txt": "2016-06-20 18:00:00"
}, {
"dt": 1466456400,
"main": {
"temp": 300.708,
"temp_min": 300.708,
"temp_max": 300.708,
"pressure": 1018.73,
"sea_level": 1019.13,
"grnd_level": 1018.73,
"humidity": 82,
"temp_kf": 0
},
"weather": [{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04n"
}],
"clouds": {
"all": 68
},
"wind": {
"speed": 1.66,
"deg": 141.5
},
"rain": {},
"sys": {
"pod": "n"
},
"dt_txt": "2016-06-20 21:00:00"
}, {
"dt": 1466467200,
"main": {
"temp": 301.217,
"temp_min": 301.217,
"temp_max": 301.217,
"pressure": 1020.18,
"sea_level": 1020.61,
"grnd_level": 1020.18,
"humidity": 79,
"temp_kf": 0
},
"weather": [{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04d"
}],
"clouds": {
"all": 68
},
"wind": {
"speed": 1.61,
"deg": 91.0013
},
"rain": {},
"sys": {
"pod": "d"
},
"dt_txt": "2016-06-21 00:00:00"
}, {
"dt": 1466478000,
"main": {
"temp": 306.368,
"temp_min": 306.368,
"temp_max": 306.368,
"pressure": 1020.81,
"sea_level": 1021.3,
"grnd_level": 1020.81,
"humidity": 62,
"temp_kf": 0
},
"weather": [{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "03d"
}],
"clouds": {
"all": 36
},
"wind": {
"speed": 2.86,
"deg": 298.001
},
"rain": {},
"sys": {
"pod": "d"
},
"dt_txt": "2016-06-21 03:00:00"
}, {
"dt": 1466488800,
"main": {
"temp": 308.988,
"temp_min": 308.988,
"temp_max": 308.988,
"pressure": 1019.11,
"sea_level": 1019.63,
"grnd_level": 1019.11,
"humidity": 49,
"temp_kf": 0
},
"weather": [{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "03d"
}],
"clouds": {
"all": 44
},
"wind": {
"speed": 4.12,
"deg": 266.505
},
"rain": {},
"sys": {
"pod": "d"
},
"dt_txt": "2016-06-21 06:00:00"
}, {
"dt": 1466499600,
"main": {
"temp": 305.343,
"temp_min": 305.343,
"temp_max": 305.343,
"pressure": 1017.43,
"sea_level": 1017.76,
"grnd_level": 1017.43,
"humidity": 67,
"temp_kf": 0
},
"weather": [{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}],
"clouds": {
"all": 36
},
"wind": {
"speed": 3.87,
"deg": 212.501
},
"rain": {
"3h": 1.415
},
"sys": {
"pod": "d"
},
"dt_txt": "2016-06-21 09:00:00"
}, {
"dt": 1466510400,
"main": {
"temp": 304.423,
"temp_min": 304.423,
"temp_max": 304.423,
"pressure": 1018.4,
"sea_level": 1018.97,
"grnd_level": 1018.4,
"humidity": 63,
"temp_kf": 0
},
"weather": [{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10n"
}],
"clouds": {
"all": 92
},
"wind": {
"speed": 2.05,
"deg": 196.508
},
"rain": {
"3h": 1.075
},
"sys": {
"pod": "n"
},
"dt_txt": "2016-06-21 12:00:00"
}, {
"dt": 1466521200,
"main": {
"temp": 303.07,
"temp_min": 303.07,
"temp_max": 303.07,
"pressure": 1020,
"sea_level": 1020.58,
"grnd_level": 1020,
"humidity": 67,
"temp_kf": 0
},
"weather": [{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04n"
}],
"clouds": {
"all": 68
},
"wind": {
"speed": 2.56,
"deg": 178.501
},
"rain": {},
"sys": {
"pod": "n"
},
"dt_txt": "2016-06-21 15:00:00"
}, {
"dt": 1466532000,
"main": {
"temp": 300.893,
"temp_min": 300.893,
"temp_max": 300.893,
"pressure": 1020.32,
"sea_level": 1020.72,
"grnd_level": 1020.32,
"humidity": 80,
"temp_kf": 0
},
"weather": [{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10n"
}],
"clouds": {
"all": 56
},
"wind": {
"speed": 1.62,
"deg": 242.5
},
"rain": {
"3h": 1.735
},
"sys": {
"pod": "n"
},
"dt_txt": "2016-06-21 18:00:00"
}, {
"dt": 1466542800,
"main": {
"temp": 300.177,
"temp_min": 300.177,
"temp_max": 300.177,
"pressure": 1019.56,
"sea_level": 1020.01,
"grnd_level": 1019.56,
"humidity": 79,
"temp_kf": 0
},
"weather": [{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04n"
}],
"clouds": {
"all": 56
},
"wind": {
"speed": 1.11,
"deg": 237.002
},
"rain": {},
"sys": {
"pod": "n"
},
"dt_txt": "2016-06-21 21:00:00"
}, {
"dt": 1466553600,
"main": {
"temp": 301.577,
"temp_min": 301.577,
"temp_max": 301.577,
"pressure": 1020.42,
"sea_level": 1020.8,
"grnd_level": 1020.42,
"humidity": 74,
"temp_kf": 0
},
"weather": [{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "03d"
}],
"clouds": {
"all": 48
},
"wind": {
"speed": 2.11,
"deg": 288.01
},
"rain": {},
"sys": {
"pod": "d"
},
"dt_txt": "2016-06-22 00:00:00"
}]
}

Answer

I think you want something like this:-

<?php
error_reporting(E_ALL); // check all type of error
ini_set('display_errors',1); // display those errors
$data = '{"city":{"id":1609350,"name":"Bangkok","coord":{"lon":100.51667,"lat":13.75},"country":"TH","population":0,"sys":{"population":0}},"cod":"200","message":0.0169,"cnt":38,"list":[{"dt":1466402400,"main":{"temp":311.79,"temp_min":308.315,"temp_max":311.79,"pressure":1018.37,"sea_level":1018.77,"grnd_level":1018.37,"humidity":61,"temp_kf":3.47},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":24},"wind":{"speed":2.14,"deg":269.001},"sys":{"pod":"d"},"dt_txt":"2016-06-20 06:00:00"},{"dt":1466413200,"main":{"temp":309.22,"temp_min":306.611,"temp_max":309.22,"pressure":1016.83,"sea_level":1017.28,"grnd_level":1016.83,"humidity":61,"temp_kf":2.6},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":36},"wind":{"speed":1.07,"deg":132.503},"rain":{"3h":1.88},"sys":{"pod":"d"},"dt_txt":"2016-06-20 09:00:00"},{"dt":1466424000,"main":{"temp":307.17,"temp_min":305.429,"temp_max":307.17,"pressure":1017.62,"sea_level":1018.09,"grnd_level":1017.62,"humidity":59,"temp_kf":1.74},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":92},"wind":{"speed":1.33,"deg":153.501},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-06-20 12:00:00"},{"dt":1466434800,"main":{"temp":303.96,"temp_min":303.092,"temp_max":303.96,"pressure":1019.69,"sea_level":1020.02,"grnd_level":1019.69,"humidity":66,"temp_kf":0.87},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":76},"wind":{"speed":4.26,"deg":180.504},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-06-20 15:00:00"},{"dt":1466445600,"main":{"temp":301.672,"temp_min":301.672,"temp_max":301.672,"pressure":1019.5,"sea_level":1019.9,"grnd_level":1019.5,"humidity":74,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":48},"wind":{"speed":3.06,"deg":163.501},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-06-20 18:00:00"},{"dt":1466456400,"main":{"temp":300.708,"temp_min":300.708,"temp_max":300.708,"pressure":1018.73,"sea_level":1019.13,"grnd_level":1018.73,"humidity":82,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":68},"wind":{"speed":1.66,"deg":141.5},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-06-20 21:00:00"},{"dt":1466467200,"main":{"temp":301.217,"temp_min":301.217,"temp_max":301.217,"pressure":1020.18,"sea_level":1020.61,"grnd_level":1020.18,"humidity":79,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":68},"wind":{"speed":1.61,"deg":91.0013},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-06-21 00:00:00"},{"dt":1466478000,"main":{"temp":306.368,"temp_min":306.368,"temp_max":306.368,"pressure":1020.81,"sea_level":1021.3,"grnd_level":1020.81,"humidity":62,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":36},"wind":{"speed":2.86,"deg":298.001},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-06-21 03:00:00"},{"dt":1466488800,"main":{"temp":308.988,"temp_min":308.988,"temp_max":308.988,"pressure":1019.11,"sea_level":1019.63,"grnd_level":1019.11,"humidity":49,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":44},"wind":{"speed":4.12,"deg":266.505},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-06-21 06:00:00"},{"dt":1466499600,"main":{"temp":305.343,"temp_min":305.343,"temp_max":305.343,"pressure":1017.43,"sea_level":1017.76,"grnd_level":1017.43,"humidity":67,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":36},"wind":{"speed":3.87,"deg":212.501},"rain":{"3h":1.415},"sys":{"pod":"d"},"dt_txt":"2016-06-21 09:00:00"},{"dt":1466510400,"main":{"temp":304.423,"temp_min":304.423,"temp_max":304.423,"pressure":1018.4,"sea_level":1018.97,"grnd_level":1018.4,"humidity":63,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"clouds":{"all":92},"wind":{"speed":2.05,"deg":196.508},"rain":{"3h":1.075},"sys":{"pod":"n"},"dt_txt":"2016-06-21 12:00:00"},{"dt":1466521200,"main":{"temp":303.07,"temp_min":303.07,"temp_max":303.07,"pressure":1020,"sea_level":1020.58,"grnd_level":1020,"humidity":67,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":68},"wind":{"speed":2.56,"deg":178.501},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-06-21 15:00:00"},{"dt":1466532000,"main":{"temp":300.893,"temp_min":300.893,"temp_max":300.893,"pressure":1020.32,"sea_level":1020.72,"grnd_level":1020.32,"humidity":80,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"clouds":{"all":56},"wind":{"speed":1.62,"deg":242.5},"rain":{"3h":1.735},"sys":{"pod":"n"},"dt_txt":"2016-06-21 18:00:00"},{"dt":1466542800,"main":{"temp":300.177,"temp_min":300.177,"temp_max":300.177,"pressure":1019.56,"sea_level":1020.01,"grnd_level":1019.56,"humidity":79,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":56},"wind":{"speed":1.11,"deg":237.002},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-06-21 21:00:00"},{"dt":1466553600,"main":{"temp":301.577,"temp_min":301.577,"temp_max":301.577,"pressure":1020.42,"sea_level":1020.8,"grnd_level":1020.42,"humidity":74,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":48},"wind":{"speed":2.11,"deg":288.01},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-06-22 00:00:00"}]}'; // your json encoded data

$json_array = json_decode($data); // decode json data
echo "<pre/>";print_r($json_array); // print original array
$final_array = array(); // take a empty array
foreach ($json_array->list as $json_arra){ // iterate loop
   if(strpos($json_arra->dt_txt,'15:00:00')){ // check if dt_txt contains 15:00:00 or not
       $final_array[$json_arra->dt_txt] = array('temp_min'=>$json_arra->main->temp_min,'temp_max'=>$json_arra->main->temp_max); // if yes then create a sub-array in the main empty array with corresponding date index
   }
}
echo "<pre/>";print_r($final_array); // print final array
?>

Output:- https://eval.in/591878

Comments