Here is the JSON being pulled in from a feed:
{
"query": {
"count": 1,
"created": "2011-12-20T14:52:50Z",
"lang": "en-US",
"results": {
"nowplaying": {
"song": {
"title": "Silent Night",
"artist": "Jaci Velasquez",
"time": "2011-12-20 09:49:51"
}
}
}
}
}
I know the feed is coming correctly. I am trying to pull in the “title” with the following, but it doesn’t seem to work:
<?php foreach($feed->query->results->nowplaying as $item) { ?>
<?php echo $item->title; ?>
<?php } ?>
I’m not sure what I am missing. Any tips?! Thanks!
How are you getting and parsing it? If you are getting it as a string, which you most likely are, you first need to parse it with
json_decode. You can specifytrueas the second parameter to get an array instead of an object, it’s easier to work with.Then you can access individual properties like an associative array. Seeing as there are no arrays in that JSON, there is no need for any loop.