how to decode this JSON string?

this is what i get as a string from a feed finder url (JSON Encoded):

{
  "updated": 1265787927,
  "id": "http://www.google.com/reader/api/0/feed-finder?q\u003dhttp://itcapsule.blogspot.com/\u0026output\u003djson",
  "title": "Feed results for \"http://itcapsule.blogspot.com/\"",
  "self": [{
    "href": "http://www.google.com/reader/api/0/feed-finder?q\u003dhttp://itcapsule.blogspot.com/\u0026output\u003djson"
  }],
  "feed": [{
    "href": "http://itcapsule.blogspot.com/feeds/posts/default"
  }]
}

How can i decode it using json_decode() function in php and get the last array element ("feed") ? i tried it with the following code but no luck

 $json = file_get_contents("http://www.google.com/reader/api/0/feed-finder?q=http://itcapsule.blogspot.com/&output=json");
 $ar = (array)(json_decode($json,true));
 print_r $ar;

Please help ..


Solution 1:

$array = json_decode($json, true);
$feed = $array['feed'];

Note that json_decode() already returns an array when you call it with true as second parameter.

Update:

As the value of feed in JSON

"feed":[{"href":"http://itcapsule.blogspot.com/feeds/posts/default"}]

is an array of objects, the content of $array['feed'] is:

Array
(
    [0] => Array
        (
            [href] => http://itcapsule.blogspot.com/feeds/posts/default
        )  
)

To get the URL you have to access the array with $array['feed'][0]['href'] or $feed[0]['href'].

But this is basic handling of arrays. Maybe the Arrays documentation helps you.