2015-02-24 26 views
0

我使用PHP(Rich Adams的php-gracenote)連接到Gracenote API並檢索有關各種專輯/ CD的數據。我已經掌握了基礎知識 - 我可以連接,創建查詢並獲取一些數據,但我對如何使用Gracenote發送的數據工作感到困惑。PHP數組 - Gracenote API

我希望能夠搜索專輯/ CD並顯示該專輯上曲目的列表。

我正在使用的代碼如下:

<?php 
include("php-gracenote/Gracenote.class.php"); 

$clientID = "XXXXXXX"; 
$clientTag = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 

$api = new Gracenote\WebAPI\GracenoteWebAPI($clientID, $clientTag); 

$userID = $api->register(); 

//$results = $api->searchAlbum("Beatles", "Revolver"); 
//var_dump($results); 

$i=0; 
while (isset($results[$i])) : 
echo $results[$i]["album_artist_name"] . '<br/>'; 
echo $results[$i]["album_title"] . ', ' . $results[$i]["album_year"] .  '<br/><br/>'; 
//echo $results[$i]["tracks"]["track_title"]; // my feeble attempt to grab relevant data 

$i++; 
endwhile; 

?> 

當我使用:

$results = $api->searchAlbum("Beatles", "Revolver"); 
var_dump($results); 

...的數據似乎是返回一個大的(多維???)陣列,我可以在數據中看到這些和那裏的曲目標題,但我無法提取並顯示它們。

我想我的問題是,我如何使用PHP中的複雜數組來提取所需的數據?

這裏的返回數據的第一部分時,我用var_dump($結果) - 有太多展現這一切):

array(10) { 
[0]=&gt; 
array(13) { 
["album_gnid"]=&gt; 
string(40) "7309349-74C2EDC353EA348726D4EDC7B705712F" 
["album_artist_name"]=&gt; 
string(11) "The Beatles" 
["album_title"]=&gt; 
string(8) "Revolver" 
["album_year"]=&gt; 
string(4) "1966" 
["genre"]=&gt; 
array(3) { 
    [0]=&gt; 
    array(2) { 
    ["id"]=&gt; 
    int(25313) 
    ["text"]=&gt; 
    string(4) "Rock" 
    } 
    [1]=&gt; 
    array(2) { 
    ["id"]=&gt; 
    int(25332) 
    ["text"]=&gt; 
    string(9) "60's Rock" 
    } 
    [2]=&gt; 
    array(2) { 
    ["id"]=&gt; 
    int(25491) 
    ["text"]=&gt; 
    string(16) "Psychedelic Rock" 
    } 
} 
["album_art_url"]=&gt; 
string(0) "" 
["artist_image_url"]=&gt; 
string(99) "http://akamai-b.cdn.cddbp.net/cds/2.0/image-artist/0907/CA7C/7B5E/3CF8_medium_front.jpg?cid=1920256" 
["artist_bio_url"]=&gt; 
string(164) "http://web.content.cddbp.net/cds/2.0?id=0AC9F1E7DEF84B87&amp;client=1920256&amp;class=biography&amp;type=text/plain&amp;tag=02-FAyNX5AA5SimoRYGji2zgKvh9LliCiQjTJOnammvNzcmKGySJQ-0g" 
["review_url"]=&gt; 
string(161) "http://web.content.cddbp.net/cds/2.0?id=F4B46CE1824CDFF5&amp;client=1920256&amp;class=review&amp;type=text/plain&amp;tag=02jCWyg580ffijjOeNpgMYD-33vNd9OY0N72B5abMJa3FKUaP3bDIZcA" 
["artist_origin"]=&gt; 
array(4) { 
    [0]=&gt; 
    array(2) { 
    ["id"]=&gt; 
    int(29894) 
    ["text"]=&gt; 
    string(14) "Western Europe" 
    } 
    [1]=&gt; 
    array(2) { 
    ["id"]=&gt; 
    int(29967) 
    ["text"]=&gt; 
    string(14) "United Kingdom" 
    } 
    [2]=&gt; 
    array(2) { 
    ["id"]=&gt; 
    int(30296) 
    ["text"]=&gt; 
    string(7) "England" 
    } 
    [3]=&gt; 
    array(2) { 
    ["id"]=&gt; 
    int(30793) 
    ["text"]=&gt; 
    string(9) "Liverpool" 
    } 
} 
["artist_era"]=&gt; 
array(1) { 
    [0]=&gt; 
    array(2) { 
    ["id"]=&gt; 
    int(29487) 
    ["text"]=&gt; 
    string(6) "1960's" 
    } 
} 
["artist_type"]=&gt; 
array(2) { 
    [0]=&gt; 
    array(2) { 
    ["id"]=&gt; 
    int(29422) 
    ["text"]=&gt; 
    string(4) "Male" 
    } 
    [1]=&gt; 
    array(2) { 
    ["id"]=&gt; 
    int(29436) 
    ["text"]=&gt; 
    string(10) "Male Group" 
    } 
} 
["tracks"]=&gt; 
array(14) { 
    [0]=&gt; 
    array(6) { 
    ["track_number"]=&gt; 
    int(1) 
    ["track_gnid"]=&gt; 
    string(40) "7309350-3A1042A706E8D9391D2728421B86C7BD" 
    ["track_title"]=&gt; 
    string(6) "Taxman" 
    ["track_artist_name"]=&gt; 
    string(11) "The Beatles" 
    ["mood"]=&gt; 
    array(0) { 
    } 
    ["tempo"]=&gt; 
    array(0) { 
    } 
    } 
    [1]=&gt; 
    array(6) { 
    ["track_number"]=&gt; 
    int(2) 
    ["track_gnid"]=&gt; 
    string(40) "7309351-8D7A9A90FA399B346AA64396FA6C697A" 
    ["track_title"]=&gt; 
    string(13) "Eleanor Rigby" 
    ["track_artist_name"]=&gt; 
    string(11) "The Beatles" 
    ["mood"]=&gt; 
    array(0) { 
    } 
    ["tempo"]=&gt; 
    array(0) { 
    } 
    } 

建議大加讚賞。

+0

您可以發表一個輸出數據的例子。很難知道如何在沒有看到它的情況下解析數組 – wilkesybear 2015-02-24 00:56:24

+0

Wilkesybear,我已經發布了一個輸出數據到前幾個曲目的例子。剩下的剩餘部分顯示太多了。 – Mekong 2015-02-24 03:17:06

+0

'foreach($ result as $ album)echo $ album ['album_title'],$ album ['album_year']; ...對於嵌套數組,執行相同的操作:'foreach($ album ['genre'] as $ genre)echo $ genre ['text'];' – deceze 2015-02-24 03:18:33

回答

1
// Loop through each item that is returned 
foreach($results as $result){ 
    // you should now be at each item. 
    echo $result['album_artist_name']; 
    echo $result['album_title']; 
    // And so on until you have echoed what you want. 

    // loop subarrays 
    foreach($result['genre'] as $genre){ 
     // again echo anything here you would like. 
     echo $genre['text']; 
    } 
} 

我的意見在這裏...使用print_r($ results)我發現它以更易讀的格式輸出數據。

+0

非常感謝,Lars。完美工作。還要感謝wilkesybear和deceze的迴應。 – Mekong 2015-02-24 05:15:20