简体   繁体   中英

How do i get the total number of comments from this JSON array using php?

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"
         }
      }
   }
}
<?php
  ...
  $count = 0;
  $array = json_decode($input, true);
  foreach($array AS $website) {
    $count += count($website['comments']['data']);
  }
  ...
?>

$count is answer.

Bonus :)

$jsonArr = ' ..your JSON array.. ';

$decodedArr = json_decode($jsonArr);

$num_comments = count($decodedArr->{'http://www.withinzambia.com/technology-and-it/your-modem-isnt-that-fast.html'}->comments->data);

echo $num_comments;

tested and works. be aware that if you load your JSON into a string, like i did here, single quotes will need to be escaped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM