douyouqian8550 2012-07-03 15:13
浏览 60
已采纳

如何使用php获取此JSON数组的注释总数?

Im tring to show a list of articles on my site with a authour, date and comments field below the article so that users can see the number of comments for an article before even opening it.

The coments are from facebook and im using the graph api which returns the following JSON code per article, how do i get the total number of comments from this? Thanks

I have tried json_decode but the arrays am getting are all with count zero.

{
   "http://www.withinzambia.com/technology-and-it/your-modem-isnt-that-fast.html": {
      "comments": {
         "data": [
            {
               "id": "10151004341202332_23086817",
               "from": {
                  "name": "Cindi Mutale",
                  "id": "1045450015732187"
               },
               "message": "Glad someone finally pointed this out.",
               "can_remove": false,
               "created_time": "2012-07-02T19:46:58+0000",
               "like_count": 0,
               "user_likes": false
            },
            {
               "id": "10151002332_23094740",
               "from": {
                  "name": "Chanda Mike",
                  "id": "1000034452054679"
               },
               "message": "my modem is 7mbps, so that's not 7MB per second?",
               "can_remove": false,
               "created_time": "2012-07-03T13:51:24+0000",
               "like_count": 0,
               "user_likes": false
            },
            {
               "id": "10151004341202332_23094782",
               "from": {
                  "name": "Precious Chulu",
                  "id": "100242343243281187"
               },
               "message": "The max for the modem in the picture is 7mbps, which is actually about 900kb when you divide by 8, so you will never download at more than 1mb per second with these modems even when MTN or Airtel upgrades the network.",
               "can_remove": false,
               "created_time": "2012-07-03T13:57:56+0000",
               "like_count": 0,
               "user_likes": false
            }
         ],
         "paging": {
            "next": "https://graph.facebook.com/10151004341202332/comments?value=1&redirect=1&limit=25&offset=25&__after_id=10151004341202332_23094782"
         }
      }
   }
}
  • 写回答

2条回答 默认 最新

  • douwenpin0428 2012-07-03 15:19
    关注
    <?php
      ...
      $count = 0;
      $array = json_decode($input, true);
      foreach($array AS $website) {
        $count += count($website['comments']['data']);
      }
      ...
    ?>
    

    $count is answer.

    Bonus :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?