Question

I am retrieving artist information from the last.fm API. Here is a sample response for U2:

{
   "artist" : {
      "stats" : {
         "playcount" : "117896887",
         "listeners" : "3077194"
      },
      "name" : "U2",
      "tags" : {
         "tag" : [
            {
               "url" : "http://www.last.fm/tag/rock",
               "name" : "rock"
            },
            {
               "url" : "http://www.last.fm/tag/classic%20rock",
               "name" : "classic rock"
            },
            {
               "url" : "http://www.last.fm/tag/irish",
               "name" : "irish"
            },
            {
               "url" : "http://www.last.fm/tag/pop",
               "name" : "pop"
            },
            {
               "url" : "http://www.last.fm/tag/alternative",
               "name" : "alternative"
            }
         ]
      },
      "image" : [
         {
            "#text" : "http://userserve-ak.last.fm/serve/34/12369741.jpg",
            "size" : "small"
         },
         {
            "#text" : "http://userserve-ak.last.fm/serve/64/12369741.jpg",
            "size" : "medium"
         },
         {
            "#text" : "http://userserve-ak.last.fm/serve/126/12369741.jpg",
            "size" : "large"
         },
         {
            "#text" : "http://userserve-ak.last.fm/serve/252/12369741.jpg",
            "size" : "extralarge"
         },
         {
            "#text" : "http://userserve-ak.last.fm/serve/_/12369741/U2.jpg",
            "size" : "mega"
         }
      ],
      "mbid" : "704acdbb-1415-4782-b0b6-0596b8c55e46",
      "similar" : {
         "artist" : [
            {
               "url" : "http://www.last.fm/music/Passengers",
               "name" : "Passengers",
               "image" : [
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/34/4826014.jpg",
                     "size" : "small"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/64/4826014.jpg",
                     "size" : "medium"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/126/4826014.jpg",
                     "size" : "large"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/252/4826014.jpg",
                     "size" : "extralarge"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/500/4826014/Passengers+Brian+Eno++U2+as++Du.jpg",
                     "size" : "mega"
                  }
               ]
            },
            {
               "url" : "http://www.last.fm/music/Bono",
               "name" : "Bono",
               "image" : [
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/34/2511353.jpg",
                     "size" : "small"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/64/2511353.jpg",
                     "size" : "medium"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/126/2511353.jpg",
                     "size" : "large"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/252/2511353.jpg",
                     "size" : "extralarge"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/_/2511353/Bono.jpg",
                     "size" : "mega"
                  }
               ]
            },
            {
               "url" : "http://www.last.fm/music/R.E.M.",
               "name" : "R.E.M.",
               "image" : [
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/34/33444847.jpg",
                     "size" : "small"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/64/33444847.jpg",
                     "size" : "medium"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/126/33444847.jpg",
                     "size" : "large"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/252/33444847.jpg",
                     "size" : "extralarge"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/500/33444847/REM+11.jpg",
                     "size" : "mega"
                  }
               ]
            },
            {
               "url" : "http://www.last.fm/music/Coldplay",
               "name" : "Coldplay",
               "image" : [
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/34/28914.jpg",
                     "size" : "small"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/64/28914.jpg",
                     "size" : "medium"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/126/28914.jpg",
                     "size" : "large"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/252/28914.jpg",
                     "size" : "extralarge"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/_/28914/Coldplay.jpg",
                     "size" : "mega"
                  }
               ]
            },
            {
               "url" : "http://www.last.fm/music/Bruce+Springsteen",
               "name" : "Bruce Springsteen",
               "image" : [
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/34/2326566.jpg",
                     "size" : "small"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/64/2326566.jpg",
                     "size" : "medium"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/126/2326566.jpg",
                     "size" : "large"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/252/2326566.jpg",
                     "size" : "extralarge"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/_/2326566/Bruce+Springsteen+bruce.jpg",
                     "size" : "mega"
                  }
               ]
            }
         ]
      },
      "streamable" : "1",
      "bandmembers" : {
         "member" : [
            {
               "yearfrom" : "1976",
               "name" : "Bono"
            },
            {
               "yearfrom" : "1976",
               "name" : "The Edge"
            },
            {
               "yearfrom" : "1976",
               "name" : "Adam Clayton"
            },
            {
               "yearfrom" : "1976",
               "name" : "Larry Mullen, Jr."
            },
            {
               "yearfrom" : "1976",
               "name" : "Dick Evans"
            },
            {
               "yearfrom" : "1976",
               "name" : "Ivan McCormick"
            },
            {
               "yearfrom" : "1976",
               "name" : "Peter Martin"
            }
         ]
      },
      "url" : "http://www.last.fm/music/U2",
      "ontour" : "0",
      "bio" : {
         "published" : "Tue, 27 Sep 2011 15:29:59 +0000",
         "yearformed" : "1976",
         "summary" : "\n U2 is an Irish rock band which formed in 1976 in Dublin, Ireland. Since the band’s formation, they have consisted of Bono</a> (real name Paul Hewson) (vocals, guitar, harmonica), The Edge</a> (real name David Evans) (guitar, keyboards, backing vocals), Adam Clayton</a> (bass) and Larry Mullen, Jr.</a> (drums, percussion). The band is the biggest selling alternative rock act of all time, having sold 140 million albums worldwide as of 2008. They have won 22 Grammys and were inducted into the Rock & Roll Hall of Fame in 2005. As of 2009, they have released 12 albums and 58 singles. \n\n Read more about U2 on Last.fm</a>.\n \n ",
         "placeformed" : "Dublin, Ireland",
         "formationlist" : {
            "formation" : {
               "yearfrom" : "1976",
               "yearto" : ""
            }
         },
         "content" : "\n U2 is an Irish rock band which formed in 1976 in Dublin, Ireland. Since the band’s formation, they have consisted of Bono</a> (real name Paul Hewson) (vocals, guitar, harmonica), The Edge</a> (real name David Evans) (guitar, keyboards, backing vocals), Adam Clayton</a> (bass) and Larry Mullen, Jr.</a> (drums, percussion). The band is the biggest selling alternative rock act of all time, having sold 140 million albums worldwide as of 2008. They have won 22 Grammys and were inducted into the Rock & Roll Hall of Fame in 2005. As of 2009, they have released 12 albums and 58 singles. \n\n Read more about U2 on Last.fm</a>.\n \n \nUser-contributed text is available under the Creative Commons By-SA License and may also be available under the GNU FDL.\n ",
         "links" : {
            "link" : {
               "rel" : "original",
               "href" : "http://www.last.fm/music/U2/+wiki",
               "#text" : ""
            }
         }
      }
   }
}

I want to display the summary only but I'm having trouble selecting it in my script. I've tried the following

 echo $result->artist->bio->summary; 

or

$output = $result->artist->bio->summary;
echo '<ul>';
foreach ($bands as $artist) {
            $name  ->name;
                    etc

}
if ($result === NULL) die('Artist not found');

If somebody could show me how to select and display one object from the returned JSON that would be great. I've tried everything I can think of or find

enter code hereobject(stdClass)[1]

public 'artist' => 
    object(stdClass)[2]
      public 'name' => string 'Foo Fighters' (length=12)
      public 'mbid' => string '67f66c07-6e61-4026-ade5-7e782fad3a5d' (length=36)
      public 'bandmembers' => 
        object(stdClass)[3]
          public 'member' => 
            array (size=7)
              ...
      public 'url' => string 'http://www.last.fm/music/Foo+Fighters' (length=37)
      public 'image' => 
        array (size=5)
          0 => 
            object(stdClass)[11]
              ...
          1 => 
            object(stdClass)[12]
              ...
          2 => 
            object(stdClass)[13]
              ...
          3 => 
            object(stdClass)[14]
              ...
          4 => 
            object(stdClass)[15]
              ...
      public 'streamable' => string '1' (length=1)
      public 'ontour' => string '0' (length=1)
      public 'stats' => 
        object(stdClass)[16]
          public 'listeners' => string '3589242' (length=7)
          public 'playcount' => string '155892081' (length=9)
      public 'similar' => 
        object(stdClass)[17]
          public 'artist' => 
            array (size=5)
              ...
      public 'tags' => 
        object(stdClass)[48]
          public 'tag' => 
            array (size=5)
              ...
      public 'bio' => 
        object(stdClass)[54]
          public 'links' => 
            object(stdClass)[55]
              ...
          public 'published' => string 'Fri, 9 Nov 2012 21:02:30 +0000' (length=30)
          public 'summary' => string '
                <a href="http://www.foofighters.com" rel="nofollow">Foo Fighters</a> are an American <a href="http://www.last.fm/tag/rock" class="bbcode_tag" rel="tag">rock</a> band formed by singer/guitarist/drummer <a href="http://www.last.fm/music/Dave+Grohl" class="bbcode_artist">Dave Grohl</a> in 1995 in Seattle, USA. Grohl formed the group as a one-man project after the dissolution of his previous band <a href="http://www.last.fm/music/Nirvana" class="bbcode_artist">Nirvana</a> in 1994. Prior to the '... (length=1384)
          public 'content' => string '
                    <a href="http://www.foofighters.com" rel="nofollow">Foo Fighters</a> are an American <a href="http://www.last.fm/tag/rock" class="bbcode_tag" rel="tag">rock</a> band formed by singer/guitarist/drummer <a href="http://www.last.fm/music/Dave+Grohl" class="bbcode_artist">Dave Grohl</a> in 1995 in Seattle, USA. Grohl formed the group as a one-man project after the dissolution of his previous band <a href="http://www.last.fm/music/Nirvana" class="bbcode_artist">Nirvana</a> in 1994. Prior to '... (length=1514)
          public 'placeformed' => string 'Seattle, USA' (length=12)
          public 'yearformed' => string '1995' (length=4)
          public 'formationlist' => 
            object(stdClass)[57]
              ...
Was it helpful?

Solution

Here is how you would parse json data:

PHP

$decodedJson = json_decode($json);

$artist = $decodedJson->{"artist"};
$bio = $artist->{"bio"};
$summary = $bio->{"summary"};
var_dump($summary);

Program Output

string(631) " U2 is an Irish rock band which formed in 1976 in Dublin, Ireland. Since the band’s formation, they have consisted of Bono (real name Paul Hewson) (vocals, guitar, harmonica), The Edge (real name David Evans) (guitar, keyboards, backing vocals), Adam Clayton (bass) and Larry Mullen, Jr. (drums, percussion). The band is the biggest selling alternative rock act of all time, having sold 140 million albums worldwide as of 2008. They have won 22 Grammys and were inducted into the Rock & Roll Hall of Fame in 2005. As of 2009, they have released 12 albums and 58 singles.

Read more about U2 on Last.fm. "

Demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top