我试图获得一个Youtube频道的信息(频道名称,描述,缩略图等)与以下PHP:
$json_file3 = file_get_contents($query);
$jfo3 = json_decode($json_file3,true);
foreach($jfo3 as $lv1) {
foreach($lv1 as $lv2) {
$yt_id = $lv2['id'];
$display_name = $lv2['snippet']['title'];
$description = nl2br($lv2['snippet']['description']);
$yt_thumbnail = $lv2['snippet']['thumbnails']['high']['url'];
}
}
这是一个来自youtube api的示例结果:
Array
(
[0] => youtube#channelListResponse
[1] => "dhbhlDw5j8dK10GxeV_UG6RSReM/dZxvylPf_SlO1WIRGaE3T4aYrfY"
[2] => Array
(
[totalResults] => 1
[resultsPerPage] => 1
)
[3] => Array
(
[0] => Array
(
[kind] => youtube#channel
[etag] => "dhbhlDw5j8dK10GxeV_UG6RSReM/XRhOiR-keld6-bc_ogKZ75nTTTs"
[id] => UCi7GJNg51C3jgmYTUwqoUXA
[snippet] => Array
(
[title] => Team Coco
[description] => Conan O'Brien presents: the official YouTube channel for CONAN on TBS.
[publishedAt] => 2008-06-23T02:45:04.000Z
[thumbnails] => Array
(
[default] => Array
(
[url] => https://yt3.ggpht.com/-fl-1HlAzrrI/AAAAAAAAAAI/AAAAAAAAAAA/pYLiLlQWlTE/s88-c-k-no/photo.jpg
)
[medium] => Array
(
[url] => https://yt3.ggpht.com/-fl-1HlAzrrI/AAAAAAAAAAI/AAAAAAAAAAA/pYLiLlQWlTE/s240-c-k-no/photo.jpg
)
[high] => Array
(
[url] => https://yt3.ggpht.com/-fl-1HlAzrrI/AAAAAAAAAAI/AAAAAAAAAAA/pYLiLlQWlTE/s240-c-k-no/photo.jpg
)
)
[localized] => Array
(
[title] => Team Coco
[description] => Conan O'Brien presents: the official YouTube channel for CONAN on TBS.
)
)
[contentDetails] => Array
(
[relatedPlaylists] => Array
(
[likes] => LLi7GJNg51C3jgmYTUwqoUXA
[favorites] => FLi7GJNg51C3jgmYTUwqoUXA
[uploads] => UUi7GJNg51C3jgmYTUwqoUXA
)
[googlePlusUserId] => 105163107119743094340
)
[statistics] => Array
(
[viewCount] => 1464321910
[commentCount] => 5760
[subscriberCount] => 2822923
[hiddenSubscriberCount] =>
[videoCount] => 3364
)
[status] => Array
(
[privacyStatus] => public
[isLinked] => 1
[longUploadsStatus] => longUploadsUnspecified
)
)
)
)
它很好地输出了我需要的所有变量,除了它还显示了这个错误:
Warning: Invalid argument supplied for foreach() in /home/vividd6/public_html/_project/charmuu/development/cms/profile_form.php on line 100
谁能告诉我如何解决这个问题?这是我第一次使用多维数组。提前感谢!
在你的数组中,第一个元素:
Array
(
[0] => youtube#channelListResponse
不是数组。foreach循环试图遍历那个,它导致了一个错误。执行一个简单的检查,以确保要循环遍历的元素是一个数组:
foreach($jfo3 as $lv1) {
if ( is_array($lv1) ) {
foreach($lv1 as $lv2) {
$yt_id = $lv2['id'];
$display_name = $lv2['snippet']['title'];
$description = nl2br($lv2['snippet']['description']);
$yt_thumbnail = $lv2['snippet']['thumbnails']['high']['url'];
}
}
}
但是您真正想要的是这个(注意,当它循环遍历数组时,$yt_id
, $display_name
, $description
和$yt_thumbnail
将被覆盖)。如果你只想从你的回复中得到一个视频,看看最底部的答案,将为你节省很多麻烦。
$json_file3 = file_get_contents($query);
$jfo3 = json_decode($json_file3,true);
//$lv1[3] contains the array you're looking for.
foreach($jfo3 as $lv1[3]) {
foreach($lv1 as $lv2) {
$yt_id = $lv2['id'];
$display_name = $lv2['snippet']['title'];
$description = nl2br($lv2['snippet']['description']);
$yt_thumbnail = $lv2['snippet']['thumbnails']['high']['url'];
}
}
如上所述,如果您只查找第一个视频,则不需要foreach循环。就用这个:
$json_file3 = file_get_contents($query);
$jfo3 = json_decode($json_file3,true);
$video = $jfo3[3][0];
$yt_id = $video['id'];
$display_name = $video['snippet']['title'];
$description = nl2br($video['snippet']['description']);
$yt_thumbnail = $video['snippet']['thumbnails']['high'];
您是否尝试在第一次foreach之后打印变量$lvl1 ?你确定变量总是一个数组吗?
使用is_array来验证变量是否为数组。
你可以使用print_r来检查变量的内容
您似乎正在尝试查找一个非数组。例如
[1] => "dhbhlDw5j8dK10GxeV_UG6RSReM/dZxvylPf_SlO1WIRGaE3T4aYrfY"
不是数组。您可以使用is_array()函数来验证项是否为数组。