3

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 ..

4
  • 2
    Apologies if I am misunderstanding but don't you just want $ar['feed'] ? Commented Feb 10, 2010 at 8:46
  • There is no need in type casting in PHP. Commented Feb 10, 2010 at 9:34
  • @Jasonbar, Yes u are correct. I need only the "feed" value. Commented Feb 10, 2010 at 10:50
  • (array) in front of json_decode() is not needed. And it does not have any effect. Commented Dec 31, 2019 at 10:45

1 Answer 1

3
$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.

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

1 Comment

@Felix, Now I am getting the result as "Array" :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.