Help with foreach

Friends can help me with this? My code returns this warning:

Notice: Trying to get property of non-object in C:\xampp\htdocs\bla.php on line 9

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\bla.php on line 9

My code:


$jsonurl = "http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&artist=cher&api_key=0d4420f96a7c0276c33b20fb5896f768&format=json";
$json = file_get_contents($jsonurl,0,null,null);
$json_saida = json_decode($json, true);

//print_r($json_saida);

foreach($json_saida->topalbums as $dados)
{

    echo $dados->name;

}

It looks as if you have established that the data is fetched, why not show us the first 10 or 20 lines of the result of your print_r() output?

Array ( [topalbums] => Array ( [album] => Array ( [0] => Array ( [name] => The Very Best of Cher [playcount] => 82885 [mbid] => 5df00011-6e57-4837-99ae-61f398481db5  => http://www.last.fm/music/Cher/The+Very+Best+of+Cher [artist] => Array ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818  => http://www.last.fm/music/Cher ) [image] => Array ( [0] => Array ( [#text] => http://userserve-ak.last.fm/serve/34s/71997588.png [size] => small ) [1] => Array ( [#text] => http://userserve-ak.last.fm/serve/64s/71997588.png [size] => medium ) [2] => Array ( [#text] => http://userserve-ak.last.fm/serve/126/71997588.png [size] => large ) [3] => Array ( [#text] => http://userserve-ak.last.fm/serve/300x300/71997588.png [size] => extralarge ) ) [@attr] => Array ( [rank] => 1 ) ) [1] => Array ( [name] => Believe [playcount] => 90387 [mbid] => 86b5434d-9479-35e3-98ca-8fbcfcf4e357  => http://www.last.fm/music/Cher/Believe [artist] => Array ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818  => http://www.last.fm/music/Cher ) [image] => Array ( [0] => Array ( [#text] => http://userserve-ak.last.fm/serve/34s/86555021.png [size] => small ) [1] => Array ( [#text] => http://userserve-ak.last.fm/serve/64s/86555021.png [size] => medium ) [2] => Array ( [#text] => http://userserve-ak.last.fm/serve/126/86555021.png [size] => large ) [3] => Array ( [#text] => http://userserve-ak.last.fm/serve/300x300/86555021.png [size] => extralarge ) ) [@attr] => Array ( [rank] => 2 ) ) [2] => Array ( [name] => Cher's Greatest Hits: 1965-1992 [playcount] => 30678 [mbid] => 063d0d31-6dcd-423b-9ed2-d3330e3e466e  => http://www.last.fm/music/Cher/Cher%27s+Greatest+Hits:+1965-1992 [artist] => Array ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818  => http://www.last.fm/music/Cher ) [image] => Array ( [0] => Array ( [#text] => http://userserve-ak.last.fm/serve/34s/84763825.png [size] => small ) [1] => Array ( [#text] => http://userserve-ak.last.fm/serve/64s/84763825.png [size] => medium ) [2] => Array ( [#text] => http://userserve-ak.last.fm/serve/126/84763825.png [size] => large ) [3] => Array ( [#text] => http://userserve-ak.last.fm/serve/300x300/84763825.png [size] => extralarge ) ) [@attr] => Array ( [rank] => 3 ) ) [3] => Array ( [name] => Strong Enough [playcount] => 10845 [mbid] => 6bcd9585-8a2c-4fe6-85e7-ab7ac631a6b3  => http://www.last.fm/music/Cher/Strong+Enough [artist] => Array ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818  => http://www.last.fm/music/Cher ) [image] => Array ( [0] => Array ( [#text] => http://userserve-ak.last.fm/serve/34s/71900078.png [size] => small ) [1] => Array ( [#text] => http://userserve-ak.last.fm/serve/64s/71900078.png [size] => medium ) [2] => Array ( [#text] => http://userserve-ak.last.fm/serve/126/71900078.png [size] => large ) [3] => Array ( [#text] => http://userserve-ak.last.fm/serve/300x300/71900078.png [size] => extralarge ) ) [@attr] => Array ( [rank] => 4 ) ) [4] => Array ( [name] => Half Breed [playcount] => 9149 [mbid] =>  => http://www.last.fm/music/Cher/Half+Breed [artist] => Array ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818  => http://www.last.fm/music/Cher ) [image] => Array ( [0] => Array ( [#text] => http://userserve-ak.last.fm/serve/34s/73100658.png [size] => small ) [1] => Array ( [#text] => http://userserve-ak.last.fm/serve/64s/73100658.png [size] => medium ) [2] => Array ( [#text] => http://userserve-ak.last.fm/serve/126/73100658.png [size] => large ) [3] => Array ( [#text] => http://userserve-ak.last.fm/serve/300x300/73100658.png [size] => extralarge ) ) [@attr] => Array ( [rank] => 5 ) ) [5] => Array ( [name] => The Greatest Hits [playcount] => 30331 [mbid] => 023af53b-2a8f-467e-aadc-307b3372ecc2  => http://www.last.fm/music/Cher/The+Greatest+Hits [artist] => Array ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818  => http://www.last.fm/music/Cher ) [image] => Array ( [0] => Array ( [#text] => http://userserve-ak.last.fm/serve/34s/71998552.png [size] => small ) [1] => Array ( [#text] => http://userserve-ak.last.fm/serve/64s/71998552.png [size] => medium ) [2] => Array ( [#text] => http://userserve-ak.last.fm/serve/126/71998552.png [size] => large ) [3] => Array ( [#text] => http://userserve-ak.last.fm/serve/300x300/71998552.png [size] => extralarge ) ) [@attr] => Array ( [rank] => 6 ) ) [6] => Array ( [name] => Heart of Stone [playcount] => 23900 [mbid] => 7468fc70-9cba-4bad-a2a8-4201221f54fe  => http://www.last.fm/music/Cher/Heart+of+Stone 

So $json_saida contains an array, and not an object

That is correct, because his [fphp]json_decode[/fphp] is telling it to use an associative array instead of the object notation.

Changing

$json_saida = json_decode($json, true);

To this:

$json_saida = json_decode($json);

Will use the object notation and likely solve the problem.

Ok, I removed the “true” the json_decode, but the warning still appears.

Please provide an updated print_r or var_dump on $json_saida

stdClass Object ( [topalbums] => stdClass Object ( [album] => Array ( [0] => stdClass Object ( [name] => The Very Best of Cher [playcount] => 82885 [mbid] => 5df00011-6e57-4837-99ae-61f398481db5 => http://www.last.fm/music/Cher/The+Very+Best+of+Cher [artist] => stdClass Object ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 => http://www.last.fm/music/Cher ) [image] => Array ( [0] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/34s/71997588.png [size] => small ) [1] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/64s/71997588.png [size] => medium ) [2] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/126/71997588.png [size] => large ) [3] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/300x300/71997588.png [size] => extralarge ) ) [@attr] => stdClass Object ( [rank] => 1 ) ) [1] => stdClass Object ( [name] => Believe [playcount] => 90387 [mbid] => 86b5434d-9479-35e3-98ca-8fbcfcf4e357 => http://www.last.fm/music/Cher/Believe [artist] => stdClass Object ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 => http://www.last.fm/music/Cher ) [image] => Array ( [0] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/34s/86555021.png [size] => small ) [1] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/64s/86555021.png [size] => medium ) [2] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/126/86555021.png [size] => large ) [3] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/300x300/86555021.png [size] => extralarge ) ) [@attr] => stdClass Object ( [rank] => 2 ) ) [2] => stdClass Object ( [name] => Cher’s Greatest Hits: 1965-1992 [playcount] => 30678 [mbid] => 063d0d31-6dcd-423b-9ed2-d3330e3e466e => http://www.last.fm/music/Cher/Cher’s+Greatest+Hits:+1965-1992 [artist] => stdClass Object ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 => http://www.last.fm/music/Cher ) [image] => Array ( [0] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/34s/84763825.png [size] => small ) [1] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/64s/84763825.png [size] => medium ) [2] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/126/84763825.png [size] => large ) [3] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/300x300/84763825.png [size] => extralarge ) ) [@attr] => stdClass Object ( [rank] => 3 ) ) [3] => stdClass Object ( [name] => Strong Enough [playcount] => 10845 [mbid] => 6bcd9585-8a2c-4fe6-85e7-ab7ac631a6b3 => http://www.last.fm/music/Cher/Strong+Enough [artist] => stdClass Object ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 => http://www.last.fm/music/Cher ) [image] => Array ( [0] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/34s/71900078.png [size] => small ) [1] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/64s/71900078.png [size] => medium ) [2] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/126/71900078.png [size] => large ) [3] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/300x300/71900078.png [size] => extralarge ) ) [@attr] => stdClass Object ( [rank] => 4 ) ) [4] => stdClass Object ( [name] => Half Breed [playcount] => 9149 [mbid] => => http://www.last.fm/music/Cher/Half+Breed [artist] => stdClass Object ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 => http://www.last.fm/music/Cher ) [image] => Array ( [0] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/34s/73100658.png [size] => small ) [1] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/64s/73100658.png [size] => medium ) [2] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/126/73100658.png [size] => large ) [3] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/300x300/73100658.png [size] => extralarge ) ) [@attr] => stdClass Object ( [rank] => 5 ) ) [5] => stdClass Object ( [name] => The Greatest Hits [playcount] => 30331 [mbid] => 023af53b-2a8f-467e-aadc-307b3372ecc2 => http://www.last.fm/music/Cher/The+Greatest+Hits [artist] => stdClass Object ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 => http://www.last.fm/music/Cher ) [image] => Array ( [0] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/34s/71998552.png [size] => small ) [1] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/64s/71998552.png [size] => medium ) [2] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/126/71998552.png [size] => large ) [3] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/300x300/71998552.png [size] => extralarge ) )

From the URL, there is no name attribute off of topalbums. You may need to use $json_saida->topalbums->album