Joseph Mok
Joseph Mok

Reputation: 97

Foreach error with multi-dimensional from YouTube APIv3

I'm trying to get a Youtube channel's information (channel name, description, thumbnail, etc) with the following 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'];
                    }
                }

And this is a sample result from 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
                            )

                    )

            )

    )

It's outputting all the variables I need nicely except that it's also showing this error:

Warning: Invalid argument supplied for foreach() in /home/vividd6/public_html/_project/charmuu/development/cms/profile_form.php on line 100

Can anyone tell me how I can fix this? It's my first time working with multi dimensional arrays. Thanks in advance!

Upvotes: 0

Views: 72

Answers (3)

Blue
Blue

Reputation: 22911

In your array, the first element:

Array
(
    [0] => youtube#channelListResponse

Is not an array. So the foreach loop is attempting to loop through that, and it's causing an error. Do a simple check to make sure the element you're attempting to loop through is an array:

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'];
        }
    }
}

But what you really want is this (Be aware, as this loops through the array, $yt_id, $display_name, $description, and $yt_thumbnail will just be overritten). See the VERY bottom answer if you only expect one video from your response, will save you a lot of hassle.

$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'];
    }
}

As stated from above, if you're only looking for the first video, then no foreach loop is needed. Just use this:

$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'];

Upvotes: 2

Anand Singh Kunwar
Anand Singh Kunwar

Reputation: 313

You seem to be trying to foreach a non-array. For example

[1] => "dhbhlDw5j8dK10GxeV_UG6RSReM/dZxvylPf_SlO1WIRGaE3T4aYrfY"

is not an array. You can use is_array() function to validate whether the item is an array or not.

Upvotes: 0

Miguel Mesquita Alfaiate
Miguel Mesquita Alfaiate

Reputation: 2968

Have you tried printing the variable $lvl1 after the first foreach? Are you sure that the variable is always an array?

Use is_array to validate if the variable is an array.

you can use print_r to check the variable's contents.

Upvotes: 0

Related Questions