PHP - 将新的JSON数据附加到现有的多维JSON

时间:2016-03-20 22:27:02

标签: php arrays json

我有一个像这样的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!"
      }
    ]
  }
]

我希望PHP能够将其他帖子附加到Feed部分,但我不确定如何。这是我到目前为止所拥有的:

$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);

此代码的目的是能够创建新帖子并将内容附加到feed标记的现有数据的顶部,然后编写新的JSON文件。

我在PHP中查找了数组中的数组示例(How to create an array for JSON using PHP?),但我没有能够使用它,因为我不知道如何在我的“for”中实现它们循环。

我希望新的JSON输出如下所示:https://gist.github.com/lavanoid/4bee9531f7bc6e3165f9

任何帮助将不胜感激! :d

2 个答案:

答案 0 :(得分:1)

我将帮助您了解如何将新帖子附加到Feed部分的逻辑。

:: Run this as admin

:: Delete the wrong junction which points to "C:\Users\{username}\Documents"
rmdir "My Documents"

:: Create a new junction to the new My Documents folder
mklink  /J  "My Documents"  "D:\MyData\Documents"

:: Change the attributes of the junction, not the folder it points to
attrib  +H  +S  +I  "My Documents"  /L

icacls "My Documents" /setowner SYSTEM  /L
icacls "My Documents" /deny Everyone:(RD)  /L

希望这可以帮到你。 :d

答案 1 :(得分:1)

这应该会帮助你。如果您有多个频道,则需要频道索引,以便将新帖子添加到其频道。
    

$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);
?>