Georg
Georg

Reputation: 648

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.

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:

{
  "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

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 

}

Upvotes: 0

Views: 238

Answers (2)

Lachlan McDonald
Lachlan McDonald

Reputation: 2424

$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

Upvotes: 1

rjz
rjz

Reputation: 16510

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.

Upvotes: 3

Related Questions