anthonyCam
anthonyCam

Reputation: 360

iTunes Search Array

I am using a wrapper that makes it very simple to grab a search term and convert it into an array.

I am having trouble parsing this array. How do I create a foreach() loop to get the values from this array?


This is the simple function that is grabbing the search term and converting to an array. The print_r() simply shows me what is being returned.:

require_once (get_template_directory() . '/Classes/itunes.php');

function itunesSearch() {
    $search_term = $_GET['s'];
    $results = iTunes::search($search_term, array(
        'country' => 'US'
    ))->results;

    print_r($results);

}

For example, let's say I search for Adele. Here is the array that is being returned:

Array ( [0] => stdClass Object ( [wrapperType] => track [kind] => song [artistId] => 262836961 [collectionId] => 1051394208 [trackId] => 1051394215 [artistName] => Adele [collectionName] => 25 [trackName] => Hello [collectionCensoredName] => 25 [trackCensoredName] => Hello [artistViewUrl] => https://itunes.apple.com/us/artist/adele/id262836961?uo=4 [collectionViewUrl] => https://itunes.apple.com/us/album/hello/id1051394208?i=1051394215&uo=4 [trackViewUrl] => https://itunes.apple.com/us/album/hello/id1051394208?i=1051394215&uo=4 [previewUrl] => http://a1912.phobos.apple.com/us/r1000/170/Music6/v4/68/34/f1/6834f1f8-8fdb-4247-492a-c0caea580082/mzaf_3920281300599106672.plus.aac.p.m4a [artworkUrl30] => http://is5.mzstatic.com/image/thumb/Music6/v4/8c/91/5d/8c915d9b-d9e4-f735-1b91-81ca1b6e6312/source/30x30bb.jpg [artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music6/v4/8c/91/5d/8c915d9b-d9e4-f735-1b91-81ca1b6e6312/source/60x60bb.jpg [artworkUrl100] => http://is5.mzstatic.com/image/thumb/Music6/v4/8c/91/5d/8c915d9b-d9e4-f735-1b91-81ca1b6e6312/source/100x100bb.jpg [collectionPrice] => 10.99 [trackPrice] => 1.29 [releaseDate] => 2015-11-20T08:00:00Z [collectionExplicitness] => notExplicit [trackExplicitness] => notExplicit [discCount] => 1 [discNumber] => 1 [trackCount] => 11 [trackNumber] => 1 [trackTimeMillis] => 295502 [country] => USA [currency] => USD [primaryGenreName] => Pop [radioStationUrl] => https://itunes.apple.com/station/idra.1051394215 [isStreamable] => )

[1] => stdClass Object ( [wrapperType] => track [kind] => song [artistId] => 262836961 [collectionId] => 420075073 [trackId] => 420075084 [artistName] => Adele [collectionName] => 21 [trackName] => Rolling in the Deep [collectionCensoredName] => 21 [trackCensoredName] => Rolling in the Deep [artistViewUrl] => https://itunes.apple.com/us/artist/adele/id262836961?uo=4 [collectionViewUrl] => https://itunes.apple.com/us/album/rolling-in-the-deep/id420075073?i=420075084&uo=4 [trackViewUrl] => https://itunes.apple.com/us/album/rolling-in-the-deep/id420075073?i=420075084&uo=4 [previewUrl] => http://a818.phobos.apple.com/us/r1000/115/Music/3d/fd/74/mzm.dqadcdcf.aac.p.m4a [artworkUrl30] => http://is5.mzstatic.com/image/thumb/Music/v4/cf/7e/47/cf7e47a8-bb18-9156-43d0-7591d0e0855e/source/30x30bb.jpg [artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music/v4/cf/7e/47/cf7e47a8-bb18-9156-43d0-7591d0e0855e/source/60x60bb.jpg [artworkUrl100] => http://is5.mzstatic.com/image/thumb/Music/v4/cf/7e/47/cf7e47a8-bb18-9156-43d0-7591d0e0855e/source/100x100bb.jpg [collectionPrice] => 10.99 [trackPrice] => 1.29 [releaseDate] => 2011-02-22T08:00:00Z [collectionExplicitness] => notExplicit [trackExplicitness] => notExplicit [discCount] => 1 [discNumber] => 1 [trackCount] => 12 [trackNumber] => 1 [trackTimeMillis] => 228293 [country] => USA [currency] => USD [primaryGenreName] => Pop [radioStationUrl] => https://itunes.apple.com/station/idra.420075084 [isStreamable] => 1 ) ...


This is what I've tried, and several variations of this:

    $results_thing = $results['stdClass']['Object'];

    foreach ($results_thing as $result) {
        echo $result['wrapperType'];

I am getting the error:

Warning: Invalid argument supplied for foreach()

Upvotes: 0

Views: 196

Answers (1)

Say that the $results holds the array you given above. Then the code for getting 'wrapperType'...

foreach($results as $result) {
$wrapper_type = $result->wrapperType;
}

Upvotes: 1

Related Questions