2013-11-26 2 views
0

itunes api에서 앨범 아트 워크를 가져 오려고하는데, 특정 노래에 대한 아트 워크를 가져와야하므로이 코드를 사용해야합니다. API에서 json을 가져 와서 배열로 바꾼 다음 반복합니다. 내가해야 할 것은 루프입니다PHP는 노래 이름이 DNA 인 다차원 배열의 일부만 가져옵니다.

Array 
(
    [resultCount] => 58 
    [results] => Array 
    (
     [0] => Array 
      (
       [wrapperType] => track 
       [kind] => song 
       [artistId] => 477515548 
       [collectionId] => 571831060 
       [trackId] => 571831159 
       [artistName] => Little Mix 
       [collectionName] => Wings - Single 
       [trackName] => Wings 
       [collectionCensoredName] => Wings - Single 
       [trackCensoredName] => Wings 
       [artistViewUrl] => https://itunes.apple.com/us/artist/little-mix/id477515548?uo=4 
       [collectionViewUrl] => https://itunes.apple.com/us/album/wings/id571831060?i=571831159&uo=4 
       [trackViewUrl] => https://itunes.apple.com/us/album/wings/id571831060?i=571831159&uo=4 
       [previewUrl] => http://a32.phobos.apple.com/us/r1000/116/Music/v4/08/61/a7/0861a7aa-b4ab-c157-b45d-420ba769e061/mzaf_6115583363349830746.aac.m4a 
       [artworkUrl30] => http://a4.mzstatic.com/us/r30/Music/v4/a7/4b/b1/a74bb1aa-af39-a128-3c81-2aaf94817537/886443701472.30x30-50.jpg 
       [artworkUrl60] => http://a1.mzstatic.com/us/r30/Music/v4/a7/4b/b1/a74bb1aa-af39-a128-3c81-2aaf94817537/886443701472.60x60-50.jpg 
       [artworkUrl100] => http://a2.mzstatic.com/us/r30/Music/v4/a7/4b/b1/a74bb1aa-af39-a128-3c81-2aaf94817537/886443701472.100x100-75.jpg 
       [collectionPrice] => 1.29 
       [trackPrice] => 1.29 
       [releaseDate] => 2012-10-12T07:00:00Z 
       [collectionExplicitness] => notExplicit 
       [trackExplicitness] => notExplicit 
       [discCount] => 1 
       [discNumber] => 1 
       [trackCount] => 1 
       [trackNumber] => 1 
       [trackTimeMillis] => 220093 
       [country] => USA 
       [currency] => USD 
       [primaryGenreName] => Pop 
       [radioStationUrl] => https://itunes.apple.com/us/station/idra.571831159 
      ) 

     [1] => Array 
      (
       [wrapperType] => track 
       [kind] => song 
       [artistId] => 477515548 
       [collectionId] => 734694154 
       [trackId] => 734694188 
       [artistName] => Little Mix 
       [collectionName] => Salute (The Deluxe Edition) 
       [trackName] => Move 
       [collectionCensoredName] => Salute (The Deluxe Edition) 
       [trackCensoredName] => Move 
       [artistViewUrl] => https://itunes.apple.com/us/artist/little-mix/id477515548?uo=4 
       [collectionViewUrl] => https://itunes.apple.com/us/album/move/id734694154?i=734694188&uo=4 
       [trackViewUrl] => https://itunes.apple.com/us/album/move/id734694154?i=734694188&uo=4 
       [previewUrl] => http://a1503.phobos.apple.com/us/r1000/011/Music4/v4/d2/88/3b/d2883b20-9e93-4b76-f8d4-fd0c0f64f4d9/mzaf_2393781884939600271.plus.aac.p.m4a 
       [artworkUrl30] => http://a1.mzstatic.com/us/r30/Music6/v4/3d/42/b1/3d42b145-82a9-0bdb-d5d3-e035d532ec21/886444313476.30x30-50.jpg 
       [artworkUrl60] => http://a5.mzstatic.com/us/r30/Music6/v4/3d/42/b1/3d42b145-82a9-0bdb-d5d3-e035d532ec21/886444313476.60x60-50.jpg 
       [artworkUrl100] => http://a3.mzstatic.com/us/r30/Music6/v4/3d/42/b1/3d42b145-82a9-0bdb-d5d3-e035d532ec21/886444313476.100x100-75.jpg 
       [trackPrice] => 1.29 
       [releaseDate] => 2013-11-05T08:00:00Z 
       [collectionExplicitness] => notExplicit 
       [trackExplicitness] => notExplicit 
       [discCount] => 1 
       [discNumber] => 1 
       [trackCount] => 16 
       [trackNumber] => 2 
       [trackTimeMillis] => 224333 
       [country] => USA 
       [currency] => USD 
       [primaryGenreName] => Pop 
       [radioStationUrl] => https://itunes.apple.com/us/station/idra.734694188 
      ) 

통해 trackName 이동과 배열을 찾은 다음 그에서 앨범 사진을 얻을 :

<?php 
    $songs = file_get_contents('https://itunes.apple.com/search?term=Little+Mix&attribute=artistTerm&entity=song&limit=300'); 

    $songs = json_decode($songs, true); 
    foreach ($songs as $v1) { 
     foreach ($v1 as $v2) { 
      foreach ($v2 as $v3) { 
       echo "$v3\n"; 
      } 
     } 
    }; 
?> 

배열의 삭감 예는 이것이다. 나는 루프를 통과 해 왔지만, 내가 원하는 노래에서 앨범 삽화 만 얻는 방법을 알 수는 없다.

감사합니다, 마커스

당신은 단지 하나의 루프가 필요

답변

1

:

foreach ($songs['results'] as $key => $song) { 
    if($song['trackName'] == 'Move'){ 
     echo sprintf('<img src="%s" />', $song['artworkUrl30'])."\n"; 
    } 
} 

Working demo.

0

무엇에 대해 :

foreach ($songs['result'] as $song) { 
    echo $song['trackName']; // or if ($song['trackName'] something) {} 
    echo $song['artworkUrl30']; // this is the url you want 
} 
1
<?php 
    $data = json_decode(file_get_contents('https://itunes.apple.com/search?term=Little+Mix&attribute=artistTerm&entity=song&limit=300', true)); 

    foreach ($data['results'] as $song) { 
     if($song['trackName'] == 'Move'){ 
      echo '<img src="' . $song['artworkUrl100'] . '" />'; 
     } 
    }; 
?> 
관련 문제