来自 YouTube APIv3 的多维 Foreach 错误

Foreach error with multi-dimensional from YouTube APIv3

我正在尝试使用以下内容获取 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

谁能告诉我如何解决这个问题?这是我第一次使用多维数组。提前致谢!

您是否尝试过在第一个 foreach 之后打印变量 $lvl1?您确定变量始终是数组吗?

使用is_array验证变量是否为数组。

您可以使用 print_r 检查变量的内容。

在您的数组中,第一个元素:

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 一个非数组。例如

[1] => "dhbhlDw5j8dK10GxeV_UG6RSReM/dZxvylPf_SlO1WIRGaE3T4aYrfY"

不是数组。您可以使用 is_array() 函数来验证该项目是否为数组。