douyanxing6054 2016-03-20 22:27 采纳率: 0%
浏览 24
已采纳

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

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

  • 写回答

2条回答 默认 最新

  • dqcuq4138 2016-03-20 23:45
    关注

    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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line