0

I"m trying to traverse on an object i"m getting from openweather.com

without the foreach() i"m getting the right result. inside the foreach() i get an error.

the php code:

 $contents = file_get_contents($url); 
 $clima=json_decode($contents,TRUE);

 echo $clima['list'][5]['main']['temp'];  // this one works

 $i=0;
 foreach($clima['list'] as $clima1) {
     echo $clima1[$i]['dt'];   // here i get PHP Notice:  Undefined offset

     echo $clima1[$i]['main']['temp']; //here i get PHP Notice:  Undefined index: list

     $i=$i+1;
 }

the error that i get is:

PHP Notice:  Undefined offset: 0   // the error is from 0 to 39 and there are 39 objects...
PHP Notice:  Undefined index: list 

the object beginning and ending part for sample:

Array ( [cod] => 200 [message] => 0.014 [cnt] => 40 [list] => 
Array ( [0] => Array ( [dt] => 1492732800 [main] => Array ( [temp] => 17.64 [temp_min] => 17.04 [temp_max] => 17.64 [pressure] => 1026.03 [sea_level] => 1031.21 [grnd_level] => 1026.03 [humidity] => 100 [temp_kf] => 0.6 ) [weather] => Array ( [0] => Array ( [id] => 800 [main] => Clear [description] => clear sky [icon] => 01n ) ) [clouds] => Array ( [all] => 0 ) [wind] => Array ( [speed] => 1.66 [deg] => 81.5008 ) [sys] => Array ( [pod] => n ) [dt_txt] => 2017-04-21 00:00:00 ) 
        [38] => Array ( [dt] => 1493143200 [main] => Array ( [temp] => 19.72 [temp_min] => 19.72 [temp_max] => 19.72 [pressure] => 1026.92 [sea_level] => 1032.02 [grnd_level] => 1026.92 [humidity] => 87 [temp_kf] => 0 ) [weather] => Array ( [0] => Array ( [id] => 800 [main] => Clear [description] => clear sky [icon] => 01n ) ) [clouds] => Array ( [all] => 0 ) [wind] => Array ( [speed] => 6.95 [deg] => 10.5008 ) [sys] => Array ( [pod] => n ) [dt_txt] => 2017-04-25 18:00:00 ) 
         [39] => Array ( [dt] => 1493154000 [main] => Array ( [temp] => 18.43 [temp_min] => 18.43 [temp_max] => 18.43 [pressure] => 1026.75 [sea_level] => 1031.91 [grnd_level] => 1026.75 [humidity] => 98 [temp_kf] => 0 ) [weather] => Array ( [0] => Array ( [id] => 800 [main] => Clear [description] => clear sky [icon] => 01n ) ) [clouds] => Array ( [all] => 0 ) [wind] => Array ( [speed] => 6.03 [deg] => 9.50076 ) [sys] => Array ( [pod] => n ) [dt_txt] => 2017-04-25 21:00:00 ) ) 
 [city] => Array ( [id] => **** [name] => ***** [coord] => Array ( [lat] => **.**** [lon] => **.**** ) [country] => ** ) )

any help to understand my mistake will be appreciated

3
  • $clima1['dt'] and $clima1['main']['temp']. Why did you even add $i there? Commented Apr 20, 2017 at 22:45
  • just remove [$i] ? Commented Apr 20, 2017 at 22:50
  • i misunderstood the output of var_dump. i removed the [$1] and now it's ok. thanks, all 3 answers helped me understand my mistake Commented Apr 21, 2017 at 12:03

3 Answers 3

1

It seems, you loop "double". The counter inside the loop is the part, which you don't need, as you already loop over the array with foreach.

I think, the following code would be the better approach

 $contents = file_get_contents($url); 
 $clima=json_decode($contents,TRUE);

 echo $clima['list'][5]['main']['temp'];  // this one works

 foreach($clima['list'] as $clima1) {
     echo $clima1['dt'];   // here i get PHP Notice:  Undefined offset

     echo $clima1['main']['temp']; //here i get PHP Notice:  Undefined index: list
 }
Sign up to request clarification or add additional context in comments.

Comments

1

That is because when you foreach over $clima['list'] you will get every values inside $clima['list']. Therefore, first, $clima1 = $clima['list'][0]. After that, $clima1 = $clima['list'][1]... Thus, $clima1 has neither 0 as an index nor 1 nor 2...

What you might do to see it more clearly is this :

foreach($clima['list'] as $key => $clima1)

And everytime, the $key will be your $id. Therefore, you can get rid of your $id and just do it like this :

foreach($clima['list'] as $id => $clima1)

Comments

1

When you run foreach($clima['list'] as $clima1) { ... each object in the loop ($clima1) is equal to $clima['list'][$i] so you don't need to manually put the $i in there.

If your really stuck I'd just run the loop like:

foreach($clima['list'] as $clima1) {
    var_dump('<pre>' . $clima1 . '</pre>');
}

To see what the $clima1 variable really is.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.