Weird array issue...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

Weird array issue...

Post by dimxasnewfrozen »

I have an array the looks like (sorry for the formatting):

Array ( [info] => Array ( [num_results] => 17 [limit] => 100 [offset] => 0 [query] => justin bieber [type] => artist [page] => 1 ) [artists] => Array ( [0] => Array ( [href] => spotify:artist:1uNFoZAHBGtllmzznpCI3s [name] => Justin Bieber [popularity] => 0.68412 ) [1] => Array ( [href] => spotify:artist:3xeiYKNLVh6M7iSh9woPKy [name] => Sean Kingston and Justin Bieber [popularity] => 0.13672 )

Code: Select all

foreach($artist_data as $d)
 {
		foreach($d as $name)
               { 
			if (isset($name['name'])) 
                       {
				echo $name['name'] . "<br/>";
			}
		}
}
What's going on though is that the first two values are "j" and "a" which isn't in the array at all. Something weird is going on with the array indexes (I think). Any ideas?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Weird array issue...

Post by Celauran »

It's because you're also iterating over $artist_data['info']. Try this:

Code: Select all

foreach ($artist_data['artists'] as $artist)
{
    if (isset($artist['name']))
    {
        echo $artist['name'] . "<br/>";
    }
}
Post Reply