PHP 数组 - Gracenote API
PHP Arrays - Gracenote API
我正在使用 PHP(Rich Adams 的 php-gracenote)连接到 Gracenote API 并检索有关各种 albums/CDs 的数据。我已经掌握了基础知识 - 我可以连接、创建查询并获取一些数据,但我对如何使用 Gracenote 发送的数据数组感到困惑。
我希望能够搜索 album/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($results) 时返回数据的第一部分 - 太多无法显示全部):
array(10) {
[0]=>
array(13) {
["album_gnid"]=>
string(40) "7309349-74C2EDC353EA348726D4EDC7B705712F"
["album_artist_name"]=>
string(11) "The Beatles"
["album_title"]=>
string(8) "Revolver"
["album_year"]=>
string(4) "1966"
["genre"]=>
array(3) {
[0]=>
array(2) {
["id"]=>
int(25313)
["text"]=>
string(4) "Rock"
}
[1]=>
array(2) {
["id"]=>
int(25332)
["text"]=>
string(9) "60's Rock"
}
[2]=>
array(2) {
["id"]=>
int(25491)
["text"]=>
string(16) "Psychedelic Rock"
}
}
["album_art_url"]=>
string(0) ""
["artist_image_url"]=>
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"]=>
string(164) "http://web.content.cddbp.net/cds/2.0?id=0AC9F1E7DEF84B87&client=1920256&class=biography&type=text/plain&tag=02-FAyNX5AA5SimoRYGji2zgKvh9LliCiQjTJOnammvNzcmKGySJQ-0g"
["review_url"]=>
string(161) "http://web.content.cddbp.net/cds/2.0?id=F4B46CE1824CDFF5&client=1920256&class=review&type=text/plain&tag=02jCWyg580ffijjOeNpgMYD-33vNd9OY0N72B5abMJa3FKUaP3bDIZcA"
["artist_origin"]=>
array(4) {
[0]=>
array(2) {
["id"]=>
int(29894)
["text"]=>
string(14) "Western Europe"
}
[1]=>
array(2) {
["id"]=>
int(29967)
["text"]=>
string(14) "United Kingdom"
}
[2]=>
array(2) {
["id"]=>
int(30296)
["text"]=>
string(7) "England"
}
[3]=>
array(2) {
["id"]=>
int(30793)
["text"]=>
string(9) "Liverpool"
}
}
["artist_era"]=>
array(1) {
[0]=>
array(2) {
["id"]=>
int(29487)
["text"]=>
string(6) "1960's"
}
}
["artist_type"]=>
array(2) {
[0]=>
array(2) {
["id"]=>
int(29422)
["text"]=>
string(4) "Male"
}
[1]=>
array(2) {
["id"]=>
int(29436)
["text"]=>
string(10) "Male Group"
}
}
["tracks"]=>
array(14) {
[0]=>
array(6) {
["track_number"]=>
int(1)
["track_gnid"]=>
string(40) "7309350-3A1042A706E8D9391D2728421B86C7BD"
["track_title"]=>
string(6) "Taxman"
["track_artist_name"]=>
string(11) "The Beatles"
["mood"]=>
array(0) {
}
["tempo"]=>
array(0) {
}
}
[1]=>
array(6) {
["track_number"]=>
int(2)
["track_gnid"]=>
string(40) "7309351-8D7A9A90FA399B346AA64396FA6C697A"
["track_title"]=>
string(13) "Eleanor Rigby"
["track_artist_name"]=>
string(11) "The Beatles"
["mood"]=>
array(0) {
}
["tempo"]=>
array(0) {
}
}
非常感谢您的建议。
// 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) 我发现它以更易读的格式输出数据。
我正在使用 PHP(Rich Adams 的 php-gracenote)连接到 Gracenote API 并检索有关各种 albums/CDs 的数据。我已经掌握了基础知识 - 我可以连接、创建查询并获取一些数据,但我对如何使用 Gracenote 发送的数据数组感到困惑。
我希望能够搜索 album/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($results) 时返回数据的第一部分 - 太多无法显示全部):
array(10) {
[0]=>
array(13) {
["album_gnid"]=>
string(40) "7309349-74C2EDC353EA348726D4EDC7B705712F"
["album_artist_name"]=>
string(11) "The Beatles"
["album_title"]=>
string(8) "Revolver"
["album_year"]=>
string(4) "1966"
["genre"]=>
array(3) {
[0]=>
array(2) {
["id"]=>
int(25313)
["text"]=>
string(4) "Rock"
}
[1]=>
array(2) {
["id"]=>
int(25332)
["text"]=>
string(9) "60's Rock"
}
[2]=>
array(2) {
["id"]=>
int(25491)
["text"]=>
string(16) "Psychedelic Rock"
}
}
["album_art_url"]=>
string(0) ""
["artist_image_url"]=>
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"]=>
string(164) "http://web.content.cddbp.net/cds/2.0?id=0AC9F1E7DEF84B87&client=1920256&class=biography&type=text/plain&tag=02-FAyNX5AA5SimoRYGji2zgKvh9LliCiQjTJOnammvNzcmKGySJQ-0g"
["review_url"]=>
string(161) "http://web.content.cddbp.net/cds/2.0?id=F4B46CE1824CDFF5&client=1920256&class=review&type=text/plain&tag=02jCWyg580ffijjOeNpgMYD-33vNd9OY0N72B5abMJa3FKUaP3bDIZcA"
["artist_origin"]=>
array(4) {
[0]=>
array(2) {
["id"]=>
int(29894)
["text"]=>
string(14) "Western Europe"
}
[1]=>
array(2) {
["id"]=>
int(29967)
["text"]=>
string(14) "United Kingdom"
}
[2]=>
array(2) {
["id"]=>
int(30296)
["text"]=>
string(7) "England"
}
[3]=>
array(2) {
["id"]=>
int(30793)
["text"]=>
string(9) "Liverpool"
}
}
["artist_era"]=>
array(1) {
[0]=>
array(2) {
["id"]=>
int(29487)
["text"]=>
string(6) "1960's"
}
}
["artist_type"]=>
array(2) {
[0]=>
array(2) {
["id"]=>
int(29422)
["text"]=>
string(4) "Male"
}
[1]=>
array(2) {
["id"]=>
int(29436)
["text"]=>
string(10) "Male Group"
}
}
["tracks"]=>
array(14) {
[0]=>
array(6) {
["track_number"]=>
int(1)
["track_gnid"]=>
string(40) "7309350-3A1042A706E8D9391D2728421B86C7BD"
["track_title"]=>
string(6) "Taxman"
["track_artist_name"]=>
string(11) "The Beatles"
["mood"]=>
array(0) {
}
["tempo"]=>
array(0) {
}
}
[1]=>
array(6) {
["track_number"]=>
int(2)
["track_gnid"]=>
string(40) "7309351-8D7A9A90FA399B346AA64396FA6C697A"
["track_title"]=>
string(13) "Eleanor Rigby"
["track_artist_name"]=>
string(11) "The Beatles"
["mood"]=>
array(0) {
}
["tempo"]=>
array(0) {
}
}
非常感谢您的建议。
// 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) 我发现它以更易读的格式输出数据。