0

The get request returns JSON like this where the data I want to access is in a map inside of the second 'data' variable where the keys are times and the values are temperatures, e.g. "2017-01-01 08:11:00: 4.8,".

{
    type: "Weather",
    name: "ISHIREMO2",
    data: {
        Temperature: {
            data: {...},

I am using

$json = file_get_contents(/*api get request here*/);

after this im trying to access this data using

foreach($json->data->Temperature->data as $key => $val) {
    /*manipulating the data here*/
}  

but when I run it i get

Warning: Invalid argument supplied for foreach()

also, when trying this with different arrays returned similar to Temperature but with a space in between, such as Wind Speed it also throws an error.

elibretto
  • 39
  • 7
  • 1
    Did you [json_decode](http://php.net/manual/en/function.json-decode.php) it first? – FirstOne Nov 27 '17 at 19:39
  • `foreach` only takes an array or object. You need `$json = json_decode($json)`. – Obsidian Age Nov 27 '17 at 19:40
  • yeah that didnt change it – elibretto Nov 27 '17 at 19:40
  • yeah sorry i missed that out of my question but i have done that and it didnt work – elibretto Nov 27 '17 at 19:42
  • `print_r(json_decode($json))` and paste the result – Ali Nov 27 '17 at 19:45
  • Array ( [0] => stdClass Object ( [type] => Weather [name] => ISHIREMO2 [data] => stdClass Object ( [Temperature] => stdClass Object ( [data] => stdClass Object ( [2017-01-01 08:11:00] => 4.8 [2017-01-01 16:43:00] => 4.8 [2017-01-01 16:33:00] => 4.9 [2017-01-01 16:13:00] => 4.9 [2017-01-01 04:25:00] => 5.6 [2017-01-01 14:49:00], is much longer but not allowed that many characters – elibretto Nov 27 '17 at 19:48

0 Answers0