Page 1 of 1

Weird array issue...

Posted: Thu Jul 26, 2012 9:53 am
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?

Re: Weird array issue...

Posted: Thu Jul 26, 2012 11:41 am
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/>";
    }
}