Daniel Winter
Daniel Winter

Reputation: 11

Getting Data from PHP Array

I have this array that i used json_decode to create. This is my code:

$obj = json_decode($json);
$results = $obj->{"results"}; // This selects the results section
echo $results[artistName];

$results is the array

The array looks like this:

array(1) { 
   [0]=> object(stdClass)#5 (8) { 
      ["wrapperType"]=> string(6) "artist"    
      ["artistType"]=> string(6) "Artist" 
      ["artistName"]=> string(5) "Human"
      ["artistLinkUrl"]=> string(56) "https://itunes.apple.com/us/artist/human/id35761368?uo=4" 
      ["artistId"]=> int(35761368) 
      ["amgArtistId"]=> int(1173165) 
      ["primaryGenreName"]=> string(3) "Pop" 
      ["primaryGenreId"]=> int(14) 
   } 
}

But it does not echo anything. Please help.

Upvotes: 1

Views: 62

Answers (2)

Austin Brunkhorst
Austin Brunkhorst

Reputation: 21130

It's still an object, so you need to access the key like this.

echo $results[0]->artistName;

Upvotes: 4

Itay Moav -Malimovka
Itay Moav -Malimovka

Reputation: 53597

echo $results[0]->artistName

Notice, u have array of stdClasses

Upvotes: 1

Related Questions