-1

I have a small issue that is driving me nuts. Through a GET Request, I obtain a nice amount of json Data from google. How in the world can I loop through it, in order to get only the ['videoID'] entries.

I would be really glad if someone could help me out on this.


$url ="https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=UUyrB0KeB5pSGPajE0RVMoRA&key=AIzaSyDaLp92MtlcSnJjFVZYjoZIs7z5Wi_P-gQ";


$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  // Disable SSL verification
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);

$json = json_decode($result,true);

print_r($json);
3
  • RTM: php.net/manual/en/control-structures.foreach.php Commented Mar 12, 2018 at 9:32
  • Please accept the answer below, If it works :) Commented Mar 12, 2018 at 9:35
  • foreach($json['items'] as $key=>$value) { echo $value['snippet']['resourceId']['videoId']."<br/>"; } This will work. I have tested it. Commented Mar 12, 2018 at 9:35

1 Answer 1

2

Please use below code:

$url ="https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=UUyrB0KeB5pSGPajE0RVMoRA&key=AIzaSyDaLp92MtlcSnJjFVZYjoZIs7z5Wi_P-gQ";


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  // Disable SSL verification
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
    $result=curl_exec($ch);
    curl_close($ch);

    $json = json_decode($result,true);

    $videoIds = array();
    foreach($json['items'] as $item){
        $videoIds[] = $item['snippet']['resourceId']['videoId'];
    }
    print_r($videoIds);

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

This worked like a Charm! Could you please explain me in Detail how you achieved this wonderful result?
Good to hear that @AlessandroCalabrese I have parsed the array and made new array with required key items.That's it :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.