0

So I'm trying to parse some JSON that the Youtube API returns. I've been breaking my head over understanding how to parse JSON in PHP and I can't seem to figure it out.

This is basically the output of the API:

{
 "kind": "youtube#channelListResponse",
 "etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/mgiCAEvrnAFhKSCga80wVfAbUtc\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#channel",
   "etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/cndol6pHIKmkRCizokwycOOUr2E\"",
   "id": "SWMb9NxQL9I6c",
   "snippet": {
    "title": "Koffee With Karan",
    "description": "Catch Karan Johar chat up Bollywood, secrets revealed and stories told , that's how the world will remember Koffee with Karan",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/sh/554333074/showposter_thumb.jpg"
     },
     "medium": {
      "url": "https://i.ytimg.com/sh/554333074/showposter.jpg"
     },
     "high": {
      "url": "https://i.ytimg.com/sh/554333074/showposter_hq.jpg"
     }
    }
   }
  }
 ]
}

What I want to get is pretty much everything in "snippet", I need the title, description and thumbnails. I've been trying to access it like this:

$getjson = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=snippet&id=SWMb9NxQL9I6c&key=mykey');
$data = json_decode($getjson,true);

echo $data['items'][0]['snippet'][0]['title'];

or even trying something like

echo $data['items']->snippet->title;

I've found numerous articles explaining how but nothing seems to work or make sense. First of all, the "items" data is outside of the starting { } brackets, which I haven't really been able to find anything about.

Any help and primarily an explanation about how this works would be awesome. I just want to understand the syntax needed to access this stuff.

s1h4d0w
  • 762
  • 6
  • 27
  • Close ... snippet isn't an array but an object, so you don't need `['snippet'][0]['title']` just `['snippet']['title']` – Jonathan Apr 02 '17 at 14:13

2 Answers2

3

You are almost there. Try $data['items'][0]['snippet']['title']

  1. json_decode(..., true). true will returns an associative array.
  2. Use [] to access array values.
  3. Anything in {} means, you can access using key, something like ['snippet'] & Anything in [] you can access using index something like [0] or [1]

This may not the good explanation. Hope it is somewhat understandable.

Saumini Navaratnam
  • 8,439
  • 3
  • 42
  • 70
  • using the second parameter of `json_decode` as `true` makes the output as object not array. – Alireza Zojaji Apr 02 '17 at 14:15
  • @AmirZojaji From manual `When TRUE, returned objects will be converted into associative arrays.`. I'm sure `true` will convert into object into array. Please correct me if I'm wrong – Saumini Navaratnam Apr 02 '17 at 14:21
  • Aaah, awesome! I already knew that adding true would return it as an array. Before I was using `$data['items'][0]['snippet'][0]['title']` and it kept saying unknown index 0, so I guessed it was the first 0 in there. Also many thanks to @AmirZojaji ! – s1h4d0w Apr 02 '17 at 14:21
0

In your code items has an array value, but snippet is a structure not an array. So must use:

echo $data->items[0]->snippet->title;

or if you have second parameter of json_decode as true you can write:

echo $data['items'][0]['snippet']['title'];
Alireza Zojaji
  • 802
  • 2
  • 13
  • 33