How do I get Javascript and PHP to order a JSON response the same way?
I have this Javascript which receives a JSON from the Google API server,
and then outputs the content (note my code has been simplified for
clarity):
function init() {
gapi.client.setApiKey('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
gapi.client.load('blogger', 'v2', function() {
var request = gapi.client.blogger.posts.list({
'blogId': 'xxxxxxxxxxxxxxxxxxxxxxx',
'fields': 'items(content,title)'
});
request.execute(function(response) {
for (var i = 0; i < response.items.length; i++)
{
$(aDiv).append(response.items[i].title);
}
});
});
}
And I have this PHP code which does essentially the same thing:
$recentBlog =
json_decode(file_get_contents("https://www.googleapis.com/blogger/v3/blogs/xxxxxxxxxxxxxxxxxxxxxx/posts?key=xxxxxxxxxxxxxxxxx"),
true);
foreach ($recentBlog['items'] as $item)
{
echo $item[`title`];
}
The problem is that the output from each code is in a different order. The
Javascript outputs to the page in the order, from top to bottom, 2, 1, 0.
The PHP goes, from top to bottom, 0, 1, 2. What I need is for the "latest"
item to be at the top, so I need both to be, in order of top to bottom, 2,
1, 0.
Other than the different methods of calling for the JSON data, which I
assume brings back the exact same response, it seems that the only real
difference is that one is a for() loop, and the other is a foreach() loop.
I can't see how that would make a difference, so there must be something
else I'm missing.
Whatever the cause, in my current situation, I need the PHP to conform to
the Javascript's ordering, so that most recent is at the top of the page.
Or, in other words, going from top to bottom, 2, 1, 0. How do I get that
to happen?
No comments:
Post a Comment