Search code examples
phparraysitunes

Return specific results from array


I am trying to return iTunes store data, which I have managed to do successfully.

I found this: https://github.com/ijanerik/PHP-iTunes-API, which is great!

The only thing is I'm not sure of is how to print individual values.

For example my code looks like:

include("classes/itunes.php");

$albums = iTunes::lookup(304576306, 'id', array(
    'entity' => 'song'
))->results;

// var_dump($albums);
print_r($albums);

Which returns all the results: http://joshrodg.com/music/test/index.php.

How would I return just trackName or artistName from the array?

The example shows how to perform the lookup, just not how to get the individual data that gets returned.

I've tried a foreach, but maybe I'm doing it wrong:

foreach($albums as $album):
    echo $album['trackName'];
    echo $album['artistName'];
endforeach;

Everytime I try that, the page whitescreens.

If I just try a echo $album['trackName']; or echo $albums['trackName']; the page also whitescreens.

So far the only way I have been able to return data is by using: print_r($albums); or var_dump($albums);

I know this is simple, just not sure what I'm missing here.


Solution

  • Try this:

        foreach ($albums as $a) {
            if ($a->wrapperType == 'track') { 
                echo $a->trackName;
                echo $a->artistName;
            }            
        }