Reputation: 45
I have a JSON file like this
[
{
"channel": "RSS Channel",
"description": "RSS Description",
"publish_date": "Wed, 17 Feb 2016 16:21:30 +0000",
"last_build_date": "Wed, 17 Feb 2016 16:21:30 +0000",
"generator": "A Human",
"link": "mysite.com",
"atom_link": "http://example.com/index.rss",
"feed": [
{
"post_id": "3",
"title": "Some RSS feed 3...",
"publish_date": "Wed, 17 Feb 2016 16:30:55 +0000",
"link": "http://example.com/#post3",
"guid": "http://example.com/#post3",
"author": "WhoAmI?",
"content": "More!"
},
{
"post_id": "2",
"title": "Some RSS feed 2...",
"publish_date": "Wed, 17 Feb 2016 16:30:55 +0000",
"link": "http://example.com/#post3",
"guid": "http://example.com/#post3",
"author": "WhoAmI?",
"content": "More!"
},
{
"post_id": "1",
"title": "Some RSS feed 1...",
"publish_date": "Wed, 17 Feb 2016 16:30:55 +0000",
"link": "http://example.com/#post3",
"guid": "http://example.com/#post3",
"author": "WhoAmI?",
"content": "More!"
}
]
}
]
I want PHP to be able to append another post to the feed section but I'm not sure how. Here is what I have by far:
$data = json_decode($json, true);
$channel_no = 0;
$channel_name = $data[$channel_no]['channel'];
$channel_description = $data[$channel_no]['description'];
$channel_lastbuilddate = $data[$channel_no]['last_build_date'];
$channel_publishdate = $data[$channel_no]['publish_date'];
$channel_generator = $data[$channel_no]['generator'];
$channel_link = $data[$channel_no]['link'];
$channel_atomlink = $data[$channel_no]['atom_link'];
$channel_feed = $data[$channel_no]['feed'];
$channel_feedlen = count($data[$channel_no]['feed']);
// ############ Get the post feed and generate a new one with the new content first ############
$post_title = "Post test";
$post_content = "Yay, it worked! :D";
$post_id = $channel_feedlen + 1;
$post_link = $domain . "/#" . $post_id;
$post_guid = $domain . "/#" . $post_id;
$post_author = "Someone";
$new_postArray = array('post_id' => $post_id, 'title' => $post_title, 'publish_date' => $today, 'link' => $post_link, 'guid' => $post_guid, 'author' => $post_author, 'content' => $post_content); // Array of new post content.
for($x = 0; $x < $channel_feedlen; $x++) { // Read through existing posts that are currently in the .JSON
$feed_id = $channel_feed[$x]['post_id'];
$feed_title = $channel_feed[$x]['title'];
$feed_publishdate = $channel_feed[$x]['publish_date'];
$feed_link = $channel_feed[$x]['link'];
$feed_guid = $channel_feed[$x]['guid'];
$feed_author = $channel_feed[$x]['author'];
$feed_content = $channel_feed[$x]['content'];
$post_array = array('post_id' => $feed_id, 'title' => $feed_title, 'publish_date' => $feed_publishdate, 'link' => $feed_link, 'guid' => $feed_guid, 'author' => $feed_author, 'content' => $feed_content);
}
$feed = json_encode($post_array);
$new_json = array('channel' => $channel_name, 'description' => $channel_description, 'last_build_date' => $channel_lastbuilddate, 'publish_date' => $channel_publishdate, 'generator' => $channel_generator, 'link' => $channel_link, 'atom_link' => $channel_atomlink, 'feed' => array($feed));
$encoded_json = json_encode($new_json, JSON_PRETTY_PRINT);
exit($encoded_json);
The aim of this code is to be able to make new posts and append the content to the top of the existing data of the feed tag, then write a new JSON file.
I've looked around for examples ( How to create an array for JSON using PHP? ) of arrays inside of an array in PHP but non of which I was able to get working as I'm not sure how to implement them in my "for" loop.
I want the new JSON output to look like this: https://gist.github.com/lavanoid/4bee9531f7bc6e3165f9
Any help would be appreciated! :D
Upvotes: 0
Views: 148
Reputation: 51
This should help you out. If you have more than one channel you'll need the index of the channel so you can add the new post to it's feed.
$json = '[
{
"channel": "RSS Channel",
"description": "RSS Description",
"publish_date": "Wed, 17 Feb 2016 16:21:30 +0000",
"last_build_date": "Wed, 17 Feb 2016 16:21:30 +0000",
"generator": "A Human",
"link": "mysite.com",
"atom_link": "http://example.com/index.rss",
"feed": [
{
"post_id": "3",
"title": "Some RSS feed 3...",
"publish_date": "Wed, 17 Feb 2016 16:30:55 +0000",
"link": "http://example.com/#post3",
"guid": "http://example.com/#post3",
"author": "WhoAmI?",
"content": "More!"
},
{
"post_id": "2",
"title": "Some RSS feed 2...",
"publish_date": "Wed, 17 Feb 2016 16:30:55 +0000",
"link": "http://example.com/#post3",
"guid": "http://example.com/#post3",
"author": "WhoAmI?",
"content": "More!"
},
{
"post_id": "1",
"title": "Some RSS feed 1...",
"publish_date": "Wed, 17 Feb 2016 16:30:55 +0000",
"link": "http://example.com/#post3",
"guid": "http://example.com/#post3",
"author": "WhoAmI?",
"content": "More!"
}
]
}
]';
$data = json_decode($json); // converts json to php array of channel objects
$post_title = "Post test";
$post_content = "Yay, it worked! :D";
$post_id = count($data[0]->feed) + 1;
$post_link = $domain . "/#" . $post_id;
$post_guid = $domain . "/#" . $post_id;
$post_author = "Someone";
$new_postArray = array('post_id' => $post_id, 'title' => $post_title, 'publish_date' => $today, 'link' => $post_link, 'guid' => $post_guid, 'author' => $post_author, 'content' => $post_content); // Array of new post content.
// Add new post to BEGINNING of feed
$data[0]->feed = $new_postArray + $data[0]->feed;
$encoded_json = json_encode($data, JSON_PRETTY_PRINT);
exit($encoded_json);
?>
Upvotes: 1
Reputation: 1681
I'll help you with the logic how to append new post to the feed section.
$your_data = json_decode($your_json);
after this you have your json with array format..
$your_data[0]; // will be your index channel
$your_data[$channel_no]['feed']; // will be your feed section, and this an array format. If you JUST want to add the new post to this feed section, you can use array_push.
$new_postArray = array('post_id' => $post_id, 'title' => $post_title, 'publish_date' => $today, 'link' => $post_link, 'guid' => $post_guid, 'author' => $post_author, 'content' => $post_content); // Array of new post content.
// To append the new post to the feed section:
array_push($your_data[$channel_no]['feed'],$new_postArray);
$encoded_json = json_encode($your_data, JSON_PRETTY_PRINT);
exit($encoded_json);
// Just like that..
Hope this help you out. :D
Upvotes: 1