简体   繁体   English

PHP-计算数组中的字符串

[英]PHP - Counting Strings in Array

Does anyone know how to get the number of occurences of "photo" and "status"? 有谁知道如何获得“照片”和“状态”的发生次数? I tried to output it as a string and perform a substr_count but no luck. 我试图将其输出为字符串并执行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 ) 

The original JSON code from the Facebook Graph API is: 来自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
    },

I perform a json_decode with the JSON output and then loop through like that 我用JSON输出执行json_decode,然后像这样循环遍历

foreach ($page_posts_overview->data as $page_posts_overview_output){ foreach($ page_posts_overview-> data as $ 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 

} }

I think you would need to loop through the array manually and keep count of the number of times each word shows up: 我认为您需要手动遍历数组,并记下每个单词出现的次数:

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

Assuming you have an array of keywords in $fb_array , $keywords should now contain a count for each word. 假设$fb_array有一个关键字数组,则$keywords现在应该包含每个单词的计数。

$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 http://www.php.net/manual/en/function.array-count-values.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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