Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count occurrences of a value in a column of an array of object arrays

Tags:

Does anyone know how to count the occurrences of "photo" in this array:

    Array ( 
    [0] => stdClass Object ( [type] => photo [id] => 1404781893036 [created_time] => 2012-03-02T07:58:23+0000 ) 
    [1] => stdClass Object ( [type] => photo [id] => 14047818930362 [created_time] => 2012-03-01T14:58:53+0000 ) 
    [2] => stdClass Object ( [type] => status [id] => 1404781893036 [created_time] => 2012-03-01T09:49:40+0000 ) 
    [3] => stdClass Object ( [type] => status [id] => 14047818930362 [created_time] => 2012-03-01T09:36:04+0000 ) 
    [4] => stdClass Object ( [type] => photo [id] => 14047818930362 [created_time] => 2012-02-28T07:03:25+0000 ) 
    [5] => stdClass Object ( [type] => photo [id] => 1404781893036 [created_time] => 2012-02-27T09:15:34+0000 ) 
    [6] => stdClass Object ( [type] => photo [id] => 14047818930362 [created_time] => 2012-02-27T07:32:13+0000 ) 
    [7] => stdClass Object ( [type] => status [id] => 1404781893036 [created_time] => 2012-02-25T09:36:57+0000 )
    [8] => stdClass Object ( [type] => photo [id] => 1404781893036 [created_time] => 2012-02-23T08:46:43+0000 ) 
    [9] => stdClass Object ( [type] => status [id] => 1404781893036 [created_time] => 2012-02-22T21:04:30+0000 ) 
    [10] => stdClass Object ( [type] => status [id] => 1404781893036 [created_time] => 2012-02-21T20:38:27+0000 )
    [11] => stdClass Object ( [type] => photo [id] => 1404781893036 [created_time] => 2012-02-21T07:22:44+0000 ) 
    [12] => stdClass Object ( [type] => status [id] => 14047818930362 [created_time] => 2012-02-20T08:32:46+0000 ) 
    [13] => stdClass Object ( [type] => status [id] => 1404781893036 [created_time] => 2012-02-17T15:00:11+0000 ) )
like image 577
Georg Avatar asked Mar 07 '12 10:03

Georg


People also ask

How do you count occurrences in an array?

To count the occurrences of each element in an array: Declare a variable that stores an empty object. Use the for...of loop to iterate over the array. On each iteration, increment the count for the current element if it exists or initialize the count to 1 .

How do you count occurrences in JavaScript?

In JavaScript, we can count the string occurrence in a string by counting the number of times the string present in the string. JavaScript provides a function match(), which is used to generate all the occurrences of a string in an array.

How many times a value appears in an array JavaScript?

To check how many times an element appears in an array: Declare a count variable and set its value to 0 . Use the forEach() method to iterate over the array. Check if the current element is equal to the specific value. If the condition is met, increment the count by 1 .


1 Answers

To count matching occurance of a string in multidimensional array you will need to iterate over each array element and match the string and increment the count. Similiarly @Dor has suggested

$count = 0;
foreach ($array as $item) {
  if ($item->type === 'photo') {
    $count++;
  }
}

If you want achieve same in single dimensional array then It's pretty straightforward. You can use array_count_values PHP array function as explained below.

<?php
   $array = array(1, "test", 1, "php", "test");
   print_r(array_count_values($array));
?>

The above example will output:

Array
(
    [1] => 2
    [test] => 2
    [php] => 1
)
like image 177
Roopendra Avatar answered Oct 05 '22 18:10

Roopendra