PHP只获取多维数组中歌曲名称为DNA的部分


PHP get only the part of a mulit-dimensional array where the song name is DNA

我正试图从itunes api中获取专辑艺术作品,我需要获取某首歌曲的艺术作品,因此我使用此代码从api中检索json,将其转换为数组,然后循环通过。

<?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";
            }
        }
    };
?>

阵列的一个精简示例是:

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 Move循环查找数组,然后从中获取专辑插图。我已经设法循环浏览了一遍,但我看不出如何只从我想要的歌曲中获得专辑艺术作品。

谢谢,Marcus

您只需要一个循环:

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

工作演示

<?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'] . '" />';
        }
    };
?>

关于:

foreach ($songs['result'] as $song) {
    echo $song['trackName']; // or if ($song['trackName'] something) {}
    echo $song['artworkUrl30']; // this is the url you want
}