0

Possible Duplicate:
How to access object properties with names like integers?

I tried to decode the json result from youtube data api by the following code:

 $url="http://gdata.youtube.com/feeds/api/videos/$id?v=2&alt=jsonc";
    echo "$url".'<BR>';
    $json = file_get_contents($url,0,null,null);
    $json_output = json_decode($json);
    $items=$json_output -> data;

    $content = "{$items->content->1}";
    echo $content.'<BR>';

Everything works fine but the last two lines. Could someone please help?

And Here is the json result:

 {"apiVersion":"2.1","data":{"id":"9jDg3Dh28rE","uploaded":"2012-10-04T03:45:49.000Z",
........
 "content":{"5":"http://www.youtube.com/v/9jDg3Dh28rE?version=3&f=videos&app=youtube_gdata",
 "1":"rtsp://v5.cache8.c.youtube.com/CiILENy73wIaGQmx8nY43OAw9hMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
 "6":"rtsp://v5.cache4.c.youtube.com/CiILENy73wIaGQmx8nY43OAw9hMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"},"duration":2403,......}}
miken32
  • 42,008
  • 16
  • 111
  • 154
user1659006
  • 373
  • 2
  • 13
  • If you post code, please reduce he example to a minimum. e.g. if everything works, but the last two lines, those are especially interesting, other lines less (I removed some FYI). Also searching for a similar question is often more helpful. – hakre Oct 05 '12 at 09:09

1 Answers1

1

You need to wrap the numeric property with {} to access it.

$content = $items->content->{1};

And you also don't need to use double quotes like below:

$thumbnail = "{$items->thumbnail->sqDefault}";

This should just be

$thumbnail = $items->thumbnail->sqDefault;
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • I think you've been here long enough to know how *searching for duplicates* works. – Leigh Oct 05 '12 at 08:54
  • @Leigh Yes, but I need to say the wrong using of double quotes. – xdazz Oct 05 '12 at 08:57
  • @xdazz: Actually that is not wrong. One might think it's superfluous but it ain't wrong. – hakre Oct 05 '12 at 09:11
  • @hakre Yes, it's my fault. I just don't like superfluous code :) – xdazz Oct 05 '12 at 09:14
  • well it's superfluous in the answer because OP was not concerned about that part ;) anyway, it's really better to look for dupes. For that what you say is superfluous I bet some other dupe exists. I mean OP asks here about a basic language feature (okay, it's a little special), however this is basic understanding of the language syntax. please look for duplicates and vote to close. – hakre Oct 05 '12 at 09:22