SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    PHP 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($jsontrue);
        
    //print_r($json_saida);
        
    foreach($json_saida->topalbums as $dados)
    {
            
        echo 
    $dados->name;
                


  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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?

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    Array ( [topalbums] => Array ( [album] => Array ( [0] => Array ( [name] => The Very Best of Cher [playcount] => 82885 [mbid] => 5df00011-6e57-4837-99ae-61f398481db5 [url] => http://www.last.fm/music/Cher/The+Very+Best+of+Cher [artist] => Array ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 [url] => 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 [url] => http://www.last.fm/music/Cher/Believe [artist] => Array ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 [url] => 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 [url] => http://www.last.fm/music/Cher/Cher%27s+Greatest+Hits:+1965-1992 [artist] => Array ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 [url] => 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 [url] => http://www.last.fm/music/Cher/Strong+Enough [artist] => Array ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 [url] => 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] => [url] => http://www.last.fm/music/Cher/Half+Breed [artist] => Array ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 [url] => 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 [url] => http://www.last.fm/music/Cher/The+Greatest+Hits [artist] => Array ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 [url] => 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 [url] => http://www.last.fm/music/Cher/Heart+of+Stone

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    So $json_saida contains an array, and not an object

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,220
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    So $json_saida contains an array, and not an object
    That is correct, because his json_decode is telling it to use an associative array instead of the object notation.

    Changing
    PHP Code:
    $json_saida json_decode($jsontrue); 
    To this:
    PHP Code:
    $json_saida json_decode($json); 
    Will use the object notation and likely solve the problem.

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I removed the "true" the json_decode, but the warning still appears.

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,220
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Felipe Turquize View Post
    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

  8. #8
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    stdClass Object ( [topalbums] => stdClass Object ( [album] => Array ( [0] => stdClass Object ( [name] => The Very Best of Cher [playcount] => 82885 [mbid] => 5df00011-6e57-4837-99ae-61f398481db5 [url] => http://www.last.fm/music/Cher/The+Very+Best+of+Cher [artist] => stdClass Object ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 [url] => 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 [url] => http://www.last.fm/music/Cher/Believe [artist] => stdClass Object ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 [url] => 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 [url] => http://www.last.fm/music/Cher/Cher%27s+Greatest+Hits:+1965-1992 [artist] => stdClass Object ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 [url] => 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 [url] => http://www.last.fm/music/Cher/Strong+Enough [artist] => stdClass Object ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 [url] => 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] => [url] => http://www.last.fm/music/Cher/Half+Breed [artist] => stdClass Object ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 [url] => 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 [url] => http://www.last.fm/music/Cher/The+Greatest+Hits [artist] => stdClass Object ( [name] => Cher [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818 [url] => 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 ) )

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,220
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    From the URL, there is no name attribute off of topalbums. You may need to use $json_saida->topalbums->album


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •