PHP - 计算数组中的字符串

时间:2012-03-07 03:18:00

标签: php facebook-graph-api

有谁知道如何获得“照片”和“状态”的出现次数?我试图将其输出为字符串并执行substr_count但没有运气。

Array ( [0] => [1] => photo ) 
Array ( [0] => [1] => photo ) 
Array ( [0] => [1] => status ) 
Array ( [0] => [1] => photo ) 
Array ( [0] => [1] => photo ) 
Array ( [0] => [1] => photo ) 
Array ( [0] => [1] => status ) 
Array ( [0] => [1] => photo ) 
Array ( [0] => [1] => status ) 
Array ( [0] => [1] => status ) 
Array ( [0] => [1] => photo ) 
Array ( [0] => [1] => status ) 
Array ( [0] => [1] => status ) 
Array ( [0] => [1] => photo ) 
Array ( [0] => [1] => status ) 
Array ( [0] => [1] => status ) 
Array ( [0] => [1] => photo ) 
Array ( [0] => [1] => photo ) 
Array ( [0] => [1] => status ) 
Array ( [0] => [1] => status ) 
Array ( [0] => [1] => status ) 
Array ( [0] => [1] => link ) 
Array ( [0] => [1] => status ) 

来自Facebook Graph API的原始JSON代码是:

{
  "data": [
    {
      "id": "123", 
      "from": {
        "name": "Some Name", 
        "category": "Food/beverages", 
        "id": "123"
      }, 
      "picture": "a pic", 
      "link": "a link", 
      "name": "bla bla ", 
      "caption": "9 new photos", 
      "icon": "a icon", 
      "actions": [
        {
          "name": "Comment", 
          "link": "a link"
        }, 
        {
          "name": "Like", 
          "link": "a link"
        }
      ], 
      "privacy": {
        "description": "Public", 
        "value": "EVERYONE"
      }, 
      "type": "photo", 
      "object_id": "232342", 
      "created_time": "2012-03-01T14:58:53+0000", 
      "updated_time": "2012-03-02T14:12:50+0000", 
      "likes": {
        "data": [
          {
            "name": "a name", 
            "id": "1234423"
          }
        ], 
        "count": 58
      }, 
      "comments": {
        "data": [
          {
            "id": "234234", 
            "from": {
              "name": "a name", 
              "id": "23423234"
            }, 
            "message": "message bla bla", 
            "created_time": "2012-03-02T13:42:48+0000", 
            "likes": 2
          }, 
          {
            "id": "234234234", 
            "from": {
              "name": " a name", 
              "category": "Food/beverages", 
              "id": "123"
            }, 
            "message": "asfd", 
            "created_time": "2012-03-02T14:12:50+0000"
          }
        ], 
        "count": 15
      }, 
      "is_published": true
    }, 
    {
      "id": "123123", 
      "from": {
        "name": "name", 
        "category": "Food/beverages", 
        "id": "123"
      }, 
      "message": "sadfasfd", 
      "actions": [
        {
          "name": "Comment", 
          "link": "a link"
        }, 
        {
          "name": "Like", 
          "link": "a link"
        }
      ], 
      "privacy": {
        "description": "Public", 
        "value": "EVERYONE"
      }, 
      "type": "status", 
      "created_time": "2012-03-01T09:49:40+0000", 
      "updated_time": "2012-03-01T17:00:53+0000", 
      "likes": {
        "data": [
          {
            "name": "name", 
            "id": "1000"
          }
        ], 
        "count": 17
      }, 
      "comments": {
        "data": [
          {
            "id": "1404", 
            "from": {
              "name": "name", 
              "id": "1000"
            }, 
            "message": "bla bla", 
            "created_time": "2012-03-01T11:41:53+0000", 
            "likes": 2
          }, 
          {
            "id": "1404781", 
            "from": {
              "name": "name", 
              "id": "142"
            }, 
            "message": "message adsafasd", 
            "created_time": "2012-03-01T17:00:53+0000"
          }
        ], 
        "count": 9
      }, 
      "is_published": true
    },

我使用JSON输出执行json_decode,然后像那样循环

foreach($ page_posts_overview->数据为$ page_posts_overview_output){

$type = " ".$page_posts_overview_output->type;
$type = explode(" ", $type);

$type = array($type);

print_r ($type);// this is what the output posted above 

}

2 个答案:

答案 0 :(得分:3)

我认为您需要手动循环遍历数组并记录每个单词显示的次数:

function count_words($fb_array) {

  $keywords = array();

  foreach($fb_array as $element) {
    $word = $element[1];
    if (!array_key_exists($keywords, $word)) {
      $keywords[$word] = 0;
    }
    $keywords[$word]++;
  }

  return $keywords;
}

print_r($keywords);

假设$fb_array中有一系列关键字,$keywords现在应该包含每个单词的计数。

答案 1 :(得分:1)

$a = array(...); // your Facebook data
$b = array();
foreach ($a as $e) {
   $b[] = $e[1];
}
$c = array_count_values($b);
var_dump($c)

http://www.php.net/manual/en/function.array-count-values.php