0

I have this JSON array (returned from iTunes) and am looking to extract the trackName, artistName, and previewURL from it. I have experimented with various foreach iterations but I cannot seem to get anything to work appropriately.

JSON array:

{
  "resultCount": 5,
  "results": [
    {
      "wrapperType": "track",
      "kind": "song",
      "artistId": 277293880,
      "collectionId": 902180909,
      "trackId": 902180917,
      "artistName": "Lady Gaga",
      "collectionName": "Born This Way (Bonus Track Version)",
      "trackName": "Born This Way",
      "collectionCensoredName": "Born This Way (Bonus Track Version)",
      "trackCensoredName": "Born This Way",
      "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4",
      "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way\/id902180909?i=902180917&uo=4",
      "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way\/id902180909?i=902180917&uo=4",
      "previewUrl": "http:\/\/a463.phobos.apple.com\/us\/r30\/Music1\/v4\/d5\/3c\/07\/d53c0773-ff35-1fe5-9be2-815038285c08\/mzaf_4465142801402798156.plus.aac.p.m4a",
      "artworkUrl30": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/30x30bb.jpg",
      "artworkUrl60": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/60x60bb.jpg",
      "artworkUrl100": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/100x100bb.jpg",
      "collectionPrice": 10.99,
      "trackPrice": 1.29,
      "releaseDate": "2013-01-01T08:00:00Z",
      "collectionExplicitness": "notExplicit",
      "trackExplicitness": "notExplicit",
      "discCount": 1,
      "discNumber": 1,
      "trackCount": 22,
      "trackNumber": 2,
      "trackTimeMillis": 260258,
      "country": "USA",
      "currency": "USD",
      "primaryGenreName": "Pop",
      "isStreamable": true
    },
    {
      "wrapperType": "track",
      "kind": "song",
      "artistId": 277293880,
      "collectionId": 902096524,
      "trackId": 902096558,
      "artistName": "Lady Gaga",
      "collectionName": "Born This Way",
      "trackName": "Born This Way",
      "collectionCensoredName": "Born This Way",
      "trackCensoredName": "Born This Way",
      "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4",
      "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way\/id902096524?i=902096558&uo=4",
      "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way\/id902096524?i=902096558&uo=4",
      "previewUrl": "http:\/\/a459.phobos.apple.com\/us\/r30\/Music1\/v4\/c7\/2a\/83\/c72a8341-88b7-c65d-09dc-c333de92bb0c\/mzaf_4381737056492150878.plus.aac.p.m4a",
      "artworkUrl30": "http:\/\/is1.mzstatic.com\/image\/thumb\/Music3\/v4\/b9\/d2\/e8\/b9d2e809-eb0b-d2df-193b-5bc99b5e5ecf\/source\/30x30bb.jpg",
      "artworkUrl60": "http:\/\/is1.mzstatic.com\/image\/thumb\/Music3\/v4\/b9\/d2\/e8\/b9d2e809-eb0b-d2df-193b-5bc99b5e5ecf\/source\/60x60bb.jpg",
      "artworkUrl100": "http:\/\/is1.mzstatic.com\/image\/thumb\/Music3\/v4\/b9\/d2\/e8\/b9d2e809-eb0b-d2df-193b-5bc99b5e5ecf\/source\/100x100bb.jpg",
      "collectionPrice": 7.99,
      "trackPrice": 1.29,
      "releaseDate": "2011-02-11T08:00:00Z",
      "collectionExplicitness": "notExplicit",
      "trackExplicitness": "notExplicit",
      "discCount": 1,
      "discNumber": 1,
      "trackCount": 14,
      "trackNumber": 2,
      "trackTimeMillis": 260258,
      "country": "USA",
      "currency": "USD",
      "primaryGenreName": "Pop",
      "isStreamable": true
    },
    {
      "wrapperType": "track",
      "kind": "song",
      "artistId": 277293880,
      "collectionId": 902180909,
      "trackId": 902180935,
      "artistName": "Lady Gaga",
      "collectionName": "Born This Way (Bonus Track Version)",
      "trackName": "The Edge of Glory",
      "collectionCensoredName": "Born This Way (Bonus Track Version)",
      "trackCensoredName": "The Edge of Glory",
      "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4",
      "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/the-edge-of-glory\/id902180909?i=902180935&uo=4",
      "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/the-edge-of-glory\/id902180909?i=902180935&uo=4",
      "previewUrl": "http:\/\/a642.phobos.apple.com\/us\/r30\/Music4\/v4\/bb\/93\/f6\/bb93f6dd-b09f-6378-9097-e41fab0a50e7\/mzaf_2685820553066586847.plus.aac.p.m4a",
      "artworkUrl30": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/30x30bb.jpg",
      "artworkUrl60": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/60x60bb.jpg",
      "artworkUrl100": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/100x100bb.jpg",
      "collectionPrice": 10.99,
      "trackPrice": 1.29,
      "releaseDate": "2013-01-01T08:00:00Z",
      "collectionExplicitness": "notExplicit",
      "trackExplicitness": "notExplicit",
      "discCount": 1,
      "discNumber": 1,
      "trackCount": 22,
      "trackNumber": 17,
      "trackTimeMillis": 321545,
      "country": "USA",
      "currency": "USD",
      "primaryGenreName": "Pop",
      "isStreamable": true
    },
    {
      "wrapperType": "track",
      "kind": "song",
      "artistId": 277293880,
      "collectionId": 902180909,
      "trackId": 902180934,
      "artistName": "Lady Gaga",
      "collectionName": "Born This Way (Bonus Track Version)",
      "trackName": "Yo\u00fc and I",
      "collectionCensoredName": "Born This Way (Bonus Track Version)",
      "trackCensoredName": "Yo\u00fc and I",
      "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4",
      "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/yo%C3%BC-and-i\/id902180909?i=902180934&uo=4",
      "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/yo%C3%BC-and-i\/id902180909?i=902180934&uo=4",
      "previewUrl": "http:\/\/a37.phobos.apple.com\/us\/r30\/Music1\/v4\/b3\/d0\/51\/b3d05174-db69-be68-272b-1b1dd35360d9\/mzaf_7395703188902498297.plus.aac.p.m4a",
      "artworkUrl30": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/30x30bb.jpg",
      "artworkUrl60": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/60x60bb.jpg",
      "artworkUrl100": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/100x100bb.jpg",
      "collectionPrice": 10.99,
      "trackPrice": 1.29,
      "releaseDate": "2011-05-23T07:00:00Z",
      "collectionExplicitness": "notExplicit",
      "trackExplicitness": "notExplicit",
      "discCount": 1,
      "discNumber": 1,
      "trackCount": 22,
      "trackNumber": 16,
      "trackTimeMillis": 307364,
      "country": "USA",
      "currency": "USD",
      "primaryGenreName": "Pop",
      "isStreamable": true
    },
    {
      "wrapperType": "track",
      "kind": "song",
      "artistId": 277293880,
      "collectionId": 716415763,
      "trackId": 716415953,
      "artistName": "Lady Gaga",
      "collectionName": "NOW That's What I Call a Workout",
      "trackName": "Born This Way (Bimbo Jones Club Remix)",
      "collectionCensoredName": "NOW That's What I Call a Workout",
      "trackCensoredName": "Born This Way (Bimbo Jones Club Remix)",
      "collectionArtistId": 4035426,
      "collectionArtistName": "Various Artists",
      "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4",
      "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way-bimbo-jones-club-remix\/id716415763?i=716415953&uo=4",
      "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way-bimbo-jones-club-remix\/id716415763?i=716415953&uo=4",
      "previewUrl": "http:\/\/a294.phobos.apple.com\/us\/r30\/Music3\/v4\/25\/47\/e8\/2547e80d-c74c-7cf4-7905-041c7f7ec259\/mzaf_1883593890824568199.plus.aac.p.m4a",
      "artworkUrl30": "http:\/\/is4.mzstatic.com\/image\/thumb\/Music6\/v4\/a5\/ed\/99\/a5ed99e5-ca11-2a34-a72d-e86b2e11a1ba\/source\/30x30bb.jpg",
      "artworkUrl60": "http:\/\/is4.mzstatic.com\/image\/thumb\/Music6\/v4\/a5\/ed\/99\/a5ed99e5-ca11-2a34-a72d-e86b2e11a1ba\/source\/60x60bb.jpg",
      "artworkUrl100": "http:\/\/is4.mzstatic.com\/image\/thumb\/Music6\/v4\/a5\/ed\/99\/a5ed99e5-ca11-2a34-a72d-e86b2e11a1ba\/source\/100x100bb.jpg",
      "collectionPrice": 11.99,
      "trackPrice": -1,
      "releaseDate": "2012-12-18T08:00:00Z",
      "collectionExplicitness": "notExplicit",
      "trackExplicitness": "notExplicit",
      "discCount": 1,
      "discNumber": 1,
      "trackCount": 18,
      "trackNumber": 1,
      "trackTimeMillis": 284453,
      "country": "USA",
      "currency": "USD",
      "primaryGenreName": "Fitness & Workout",
      "isStreamable": false
    }
  ]
}

For example, I had tried something along the lines of:

foreach($obj['results'] as $row => $value) {
        foreach($value['trackName'] as $type => $output) {
            echo $output . "/n";
        }
    }

Where $obj is the result of json_decode. I think my brain is simply fried for today because I feel like it's not that difficult, but here we are.

Soulfire
  • 4,218
  • 23
  • 33
  • 3
    `$value['trackName']` _is_ the track name and is a string, not an array you can iterate through using foreach. That is if you passed `true` as the second argument to `json_decode()`, otherwise it's `$value->trackName`. – M. Eriksson Jul 12 '17 at 20:45
  • Yes. Also, make sure that the second parameter of json_decode is set to true like so, json_decode($jsonstring, true), or your result will be an annoying object, not a nice array – wedstrom Jul 12 '17 at 20:46
  • 1
    @wedstrom - _"annoying object"_ - LOL. :-p – M. Eriksson Jul 12 '17 at 20:47
  • Yes, `true` is passed in as the second parameter of `json_decode()` – Soulfire Jul 12 '17 at 20:48
  • Well then, just do `echo $value['trackName']`. – M. Eriksson Jul 12 '17 at 20:49
  • `$data = json_decode($json); foreach($data->results as $result){ echo $result->trackName; }` – Kevin P Jul 12 '17 at 20:49
  • How in the heck did I miss this one? That's embarrassing, but it seems to work. Swore I had tried this one but it's the end of my day. Can I blame it on that? – Soulfire Jul 12 '17 at 20:49
  • No worries. We've all been there. :-) – M. Eriksson Jul 12 '17 at 20:50
  • One other somewhat related quesiton. How would I handle special characters such as the umlaut `ü`? One of the tracks returned above is named `Yoü and I` but is echoed as `Yo\u00fc and I` – Soulfire Jul 12 '17 at 20:54
  • 1
    @MagnusEriksson That is the canonical name for the anonymous object from the json_encode :P – wedstrom Jul 12 '17 at 20:55
  • Sounds like double-encoding (encoded twice). – wedstrom Jul 12 '17 at 20:55
  • @wedstrom You learn something new everyday. :D – M. Eriksson Jul 12 '17 at 20:56
  • In case anyone finds this, changing `json_encode($array)` to `json_encode($array, JSON_UNESCAPED_UNICODE)` preserved the umlaut so now my output is as expected. Thanks again for everyone's help I truly appreciate it! – Soulfire Jul 13 '17 at 12:48

0 Answers0