0

I'm having a problem making an array for foreach(){} for the json that uses curl to get a json from youtube. I already have the json decoded for the curl part but when I tried to decode using true to get the array for json_decode, I can't seem to get the array on each of the array output. How do i get each item and put it out as an array?

I already tried this website:
Decoding JSON using PHP
to get some help but its not working.

////////youtube api////////
$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=5&order=rating&q=resident%20evil&type=video&videoEmbeddable=true&key=",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",

));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) 
    {
    echo "cURL Error #:" . $err;
} else 
    {
    echo $response;
        //echo json_decode($response, true);

        $json_array = (array) json_decode($response, true);

        foreach($json_array as $json)
            {
                //echo $json['title']; // you can access your key value like this if result is array
                //echo $json['kind'][0];
               // echo $json['id'];
                //echo $json->title; // you can access your key value like this if result is object
//                
//                $error =  json_last_error();
//                var_dump($json, $error === JSON_ERROR_UTF8);

                //print $json->response->kind[0];

                //echo $error;
            }

}
/////youtube api//////


OUTPUT JSON:

{
    "kind": "youtube#searchListResponse",
    "etag": "\"p4VTdlkQv3HQeTEaXgvLePAydmU/l9fetqKKtU8Szcemtrp1SkvVqXU\"",
    "nextPageToken": "CAUQAA",
    "regionCode": "US",
    "pageInfo": {
        "totalResults": 1000000,
        "resultsPerPage": 5
    },
    "items": [{
        "kind": "youtube#searchResult",
        "etag": "\"p4VTdlkQv3HQeTEaXgvLePAydmU/ycGvBReC7A0jFAVEINKomlikUpk\"",
        "id": {
            "kind": "youtube#video",
            "videoId": "_SzgAVZUl78"
        },
        "snippet": {
            "publishedAt": "2019-10-04T15:51:00.000Z",
            "channelId": "UCeXaWu8hgGxjnOwM2r4Zu_A",
            "title": "Resident Evil llega a Monster Hunter World: Iceborne",
            "description": "Cazadores algo sobrenatural esta sucediento en Monster Hunter: World, y es por ello que Leon y Claire de Resident Evil, llegarán al juego este noviembre ...",
            "thumbnails": {
                "default": {
                    "url": "https://i.ytimg.com/vi/_SzgAVZUl78/default.jpg",
                    "width": 120,
                    "height": 90
                },
                "medium": {
                    "url": "https://i.ytimg.com/vi/_SzgAVZUl78/mqdefault.jpg",
                    "width": 320,
                    "height": 180
                },
                "high": {
                    "url": "https://i.ytimg.com/vi/_SzgAVZUl78/hqdefault.jpg",
                    "width": 480,
                    "height": 360
                }
            },
            "channelTitle": "Arata Latam",
            "liveBroadcastContent": "none"
        }
    }, {
        "kind": "youtube#searchResult",
        "etag": "\"p4VTdlkQv3HQeTEaXgvLePAydmU/Cz2nZtIJefFeGMYIUIS52KQzRsI\"",
        "id": {
            "kind": "youtube#video",
            "videoId": "gE2EGGstC_4"
        },
        "snippet": {
            "publishedAt": "2018-02-04T18:29:52.000Z",
            "channelId": "UCaddCVMWjvDI_rfWZML8qvw",
            "title": "RESIDENT EVIL - REVELATIONS [#12] THE QUEEN IS DEAD",
            "description": "WALKTHROUGH OF ALL EPISODES OF THE QUEEN IS DEAD. NO COMMENTARY. NORMAL DIFFICULTY. HD.",
            "thumbnails": {
                "default": {
                    "url": "https://i.ytimg.com/vi/gE2EGGstC_4/default.jpg",
                    "width": 120,
                    "height": 90
                },
                "medium": {
                    "url": "https://i.ytimg.com/vi/gE2EGGstC_4/mqdefault.jpg",
                    "width": 320,
                    "height": 180
                },
                "high": {
                    "url": "https://i.ytimg.com/vi/gE2EGGstC_4/hqdefault.jpg",
                    "width": 480,
                    "height": 360
                }
            },
            "channelTitle": "Wormwoodmachine",
            "liveBroadcastContent": "none"
        }
    }, {
        "kind": "youtube#searchResult",
        "etag": "\"p4VTdlkQv3HQeTEaXgvLePAydmU/pYPZMJ3MHu9g7tghE7einGWzLSo\"",
        "id": {
            "kind": "youtube#video",
            "videoId": "4w7ISumKoe4"
        },
        "snippet": {
            "publishedAt": "2010-08-01T20:01:50.000Z",
            "channelId": "UCbaXoO4Lnw826akSES9J51w",
            "title": "Biohazard / Resident Evil - Sega Saturn",
            "description": "http://www.thearchive.tk.",
            "thumbnails": {
                "default": {
                    "url": "https://i.ytimg.com/vi/4w7ISumKoe4/default.jpg",
                    "width": 120,
                    "height": 90
                },
                "medium": {
                    "url": "https://i.ytimg.com/vi/4w7ISumKoe4/mqdefault.jpg",
                    "width": 320,
                    "height": 180
                },
                "high": {
                    "url": "https://i.ytimg.com/vi/4w7ISumKoe4/hqdefault.jpg",
                    "width": 480,
                    "height": 360
                }
            },
            "channelTitle": "Nakian",
            "liveBroadcastContent": "none"
        }
    }, {
        "kind": "youtube#searchResult",
        "etag": "\"p4VTdlkQv3HQeTEaXgvLePAydmU/uEOwZoLXlcKYQTs2Vvp-06qtS4Q\"",
        "id": {
            "kind": "youtube#video",
            "videoId": "1g2ems2cW2I"
        },
        "snippet": {
            "publishedAt": "2019-01-28T15:51:45.000Z",
            "channelId": "UCAbZF8x8TbsooEWfJ0fA-Cw",
            "title": "Resident Evil 2 Remake Last Judgment",
            "description": "Resident Evil 2 Remake (Soundtrack)",
            "thumbnails": {
                "default": {
                    "url": "https://i.ytimg.com/vi/1g2ems2cW2I/default.jpg",
                    "width": 120,
                    "height": 90
                },
                "medium": {
                    "url": "https://i.ytimg.com/vi/1g2ems2cW2I/mqdefault.jpg",
                    "width": 320,
                    "height": 180
                },
                "high": {
                    "url": "https://i.ytimg.com/vi/1g2ems2cW2I/hqdefault.jpg",
                    "width": 480,
                    "height": 360
                }
            },
            "channelTitle": "Black Pain",
            "liveBroadcastContent": "none"
        }
    }, {
        "kind": "youtube#searchResult",
        "etag": "\"p4VTdlkQv3HQeTEaXgvLePAydmU/hyeTWi-c-0xib8C7ZRuxjZGT7bE\"",
        "id": {
            "kind": "youtube#video",
            "videoId": "tud8tUdC7OM"
        },
        "snippet": {
            "publishedAt": "2019-10-04T20:06:42.000Z",
            "channelId": "UChWTmWEZBWLroVBsB9MvU2A",
            "title": "Resident Evil 2 R2MAKE Hardcore Leon B",
            "description": "Thy kingdom come, Thy will be done. We play all kinds of games. Silent hill, batman, Resident evil, metal gear, h1z1, fortnite, roblox, dead space, evil within, god ...",
            "thumbnails": {
                "default": {
                    "url": "https://i.ytimg.com/vi/tud8tUdC7OM/default.jpg",
                    "width": 120,
                    "height": 90
                },
                "medium": {
                    "url": "https://i.ytimg.com/vi/tud8tUdC7OM/mqdefault.jpg",
                    "width": 320,
                    "height": 180
                },
                "high": {
                    "url": "https://i.ytimg.com/vi/tud8tUdC7OM/hqdefault.jpg",
                    "width": 480,
                    "height": 360
                }
            },
            "channelTitle": "Tu Papi Leon",
            "liveBroadcastContent": "none"
        }
    }]
}
4
  • Please put here result of youtube api for helping better to understand what you want :) Commented Oct 4, 2019 at 20:16
  • you code is fine, also (array) before json_decode($response, true) is useless , it is already an array. maybe you have a problem with receiving json. Commented Oct 4, 2019 at 20:16
  • Your JSON is an object, not an array Commented Oct 4, 2019 at 20:21
  • items is an array right? Commented Oct 4, 2019 at 20:26

1 Answer 1

0

You are looping in the top level array. You need to loop in the items index:

foreach($json_array['items'] as $json){
    echo $json['snippet']['title']."<br>";
    echo $json['kind'];           
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.