I've been trying for a while to find out why I get an error when trying to traverse this array that the Youtube API generates (tree obtained by print_r):
Array
(
[nextPageToken] => CAIQAA
[items] => Array
(
[0] => Array
(
[snippet] => Array
(
[resourceId] => Array
(
[videoId] => sGIm0-dQd8M
)
)
)
[1] => Array
(
[snippet] => Array
(
[resourceId] => Array
(
[videoId] => VGd1ml4Hvas
)
)
)
)
)
In the first instance, I tried foreach but it generates an error:
$data_decode = json_decode($data, true); //here I get my array
foreach ($data_decode as $decode){
foreach ($decode as $code){
echo $code['snippet']['resourceId']['videoId'];
}
}
Invalid argument supplied for foreach()
I think the problem is for the "nextPageToken" node, which I need to have. What is the correct way to traverse this array?
$codeis an array that containsnextPageTokenanditemsas keys. Sosnippetis inarray[items][0]['snippet']