PHP json_decode youtube api(数组)[关闭]

I'm trying to decode the youtube api. I don't need everything, just a few pieces, but I'm having trouble with the "items" array.

youtube api snippet

My code is very very simple.

$url = file_get_contents("THIS IS MY YOUTUBE API ENDPOINT");
$json = json_decode($url, true);

If I echo out

echo $json['kind']."<p>".$json['etag']."<p>".$json['nextPageToken']."<p>";
echo $json['pageInfo']['totalResults']."<p>".$json['pageInfo']['resultsPerPage'];

Then I would see:

  • youtube#activityListResponse
  • that super long etag that I don't feel like typing
  • CAEQAA
  • 10
  • 1

But I want the information within ITEMS

If I try print_r($json['items'][0]); Then I get ALL of the information from items as an array, spit out like this

Array ( [kind] => youtube#activity [etag] => "ZG3FIn5B5vcHjQiQ9nDOCWdxwWo/9bAEAi43B5b3tiYpW1BAPw2aZ54" [id] => VTE1MjQwOTM0MjI5NDIyNjY4MzQ2NjUxMg== [snippet] => Array ( [publishedAt] => 2018-04-18T23:17:02.000Z [channelId] => UC6TEaGms62zd11sdt_z1UAg [title] => AppyBuilder: Create a High Score Leaderboard with Wheel of Fortune [description] => TUTORIAL DIFFICULTY: Medium/Hard You should be able to follow along easily if you are able to find blocks by looking at a screenshot. This tutorial uses Fusion Table as the Database to store/retrieve user data. You are required to already have a basic knowledge of how Fusion Table works in order to complete this tutorial. VIDEO DESCRIPTION: This video builds off the Fusion Table tutorials. Giving you additional examples on how to easily check for a Username/Password in the Fusion Table (Screen1), how to update the user's Score with a Spinning Wheel Event (EVENT Screen) and how to view all user's high scores (LEADERBOARD Screen). The tutorial focuses on the Leaderboard Screen which shows you how to grab the necessary information from the Fusion Table and output that data using a Custom List View. The Custom List will show the user's avatar, username and high score in order from highest to lowest. VIDEO CHAPTERS: Introduction - 00:00 Getting Started - 00:13 Design View - 01:19 Blocks Editor - 03:32 Testing the App - 07:34 Wrap Up - 08:00 Pixii Bomb Squad - 09:48 AppyBuilder Community - 09:53 Goodbye - 10:00 DOWNLOAD PROJECT .aia FILE: http://community.appybuilder.com/t/high-score-leaderboard-using-fusion-table-as-the-database/8528 FACEBOOK PAGE: https://www.facebook.com/pixiibomb PATREON PAGE: https://www.patreon.com/pixiibomb AppyBuilder: http://appybuilder.com/ AppyBuilder Community: http://community.appybuilder.com/ FREE RESOURCES Although I do create a lot of my images, to save time in Tutorials or quick projects, I like to use a free resource sites. My favorites are: http://www.freepik.com/ http://www.flaticon.com/ [thumbnails] => Array ( [default] => Array ( [url] => https://i.ytimg.com/vi/xOCI9viNun8/default.jpg [width] => 120 [height] => 90 ) [medium] => Array ( [url] => https://i.ytimg.com/vi/xOCI9viNun8/mqdefault.jpg [width] => 320 [height] => 180 ) [high] => Array ( [url] => https://i.ytimg.com/vi/xOCI9viNun8/hqdefault.jpg [width] => 480 [height] => 360 ) [standard] => Array ( [url] => https://i.ytimg.com/vi/xOCI9viNun8/sddefault.jpg [width] => 640 [height] => 480 ) [maxres] => Array ( [url] => https://i.ytimg.com/vi/xOCI9viNun8/maxresdefault.jpg [width] => 1280 [height] => 720 ) ) [channelTitle] => Pixii Bomb [type] => upload ) [contentDetails] => Array ( [upload] => Array ( [videoId] => xOCI9viNun8 ) ) ) youtube#activity"ZG3FIn5B5vcHjQiQ9nDOCWdxwWo/9bAEAi43B5b3tiYpW1BAPw2aZ54"VTE1MjQwOTM0MjI5NDIyNjY4MzQ2NjUxMg==

If I try foreach($json['items'][0] as $key=>$value){ echo $value; }

Then I get: Notice: Array to string conversion <-- Not what I want

What I want is something like

echo $json['items']['snippet']['title'];

(But of course, that doesn't work)

It sounds like you're simply looking for echo $json['items'][0]['snippet']['title'];.

If you want to loop over them, this can be done with array_walk():

function myfunction($value, $key)
{
    echo "The key $key has the value $value<br />";
}

array_walk($json, "myfunction");

Also note that your two lines:

echo $json['kind']."<p>".$json['etag']."<p>".$json['nextPageToken']."<p>";
echo $json['pageInfo']['totalResults']."<p>".$json['pageInfo']['resultsPerPage'];

Are invalid HTML; you cannot have a <p> tag nested within another <p> tag; don't forget to close them off with </p>.