$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$counts = array_count_values($array);
echo $counts['Ben'];
Answer from Rajat Singhal on Stack Overflow
🌐
PHP
php.net › manual › en › function.array-count-values.php
PHP: array_count_values - Manual
... Simple way to find number of ... 'userId' => 5], ['id' => 3, 'userId' => 6], ]; $userId = 5; echo array_count_values(array_column($list, 'userId'))[$userId]; // outputs: 2 ?>...
🌐
W3Schools
w3schools.com › php › func_array_count.asp
PHP count() Function
<?php $cars=array("Volvo","BMW","Toyota"); echo count($cars); ?> Try it Yourself » · The count() function returns the number of elements in an array or in a countable object. count(value, mode) Count the array recursively: <?php $cars=array ...
🌐
w3resource
w3resource.com › php › function-reference › array_count_values.php
PHP: array_count_values() function - w3resource
The array_count_values is used to count all the values of an array. ... An associative array. ... <?php $subject =array('Math', 20, 30 ,20,'Math', 'Science', 'Geography'); print_r(array_count_values($subject)); ?>
Top answer
1 of 3
2

As I can't comment, the philonus's answer is the clearer and best for me, if you want to display zero you can do this, initializing the stats array:

$input = array(
    "report=D","report=D","report=D","report=I","report=I"
);

$stats = array(
    'U' => 0,
    'D' => 0,
    'I' => 0,
);
foreach($input as $value) {
    $types = explode('=', $value);
    $stats[$types[1]]++;
}

print_r($stats);
2 of 3
1

I am not really an expert in PHP, but this can certainly be done a bit easier. First of all it seems as if you would need to iterate over the input array only once (while the ordering of the array does not matter). Hence you can use a foreach($array_to_work_on, $single_value) construct and do all the logic therein.

So the first thing to do is to split each value. You can use explode which takes as a first argument the string where the input value should be split and as a second the input itself. Since we are not interested in the first part we take only the second one (with the index 1).

Then you can make use of the "array" (or rather dictionary) data structure provided by PHP: Whenever we encounter an unseen "key" we add a new entry to the dictionary and save "1" as the number of already counted elements. In the other case (if we have already seen this key) we increment it by one.

So here is the code:

$input = array(
"report=D","report=D","report=D","report=I","report=I"
);

$stats = array();
foreach($input as $value) {
  $type = explode('=', $value)[1];
  $stats[$type] = array_key_exists($type,$stats) ? $stats[$type] + 1 : 1;
}

print_r($stats);

Note that now you also sum up also the frequency of other keys, not only "I", "O", and "U", which makes it easier to maintain or to extend.

Edit: As has been pointed out in the comments, one cannot use "function array dereferencing", i.e. $type = explode('=', $value)[1]; in older PHP versions. So the solution in this case would be to introduce a temporary variable:

$parts = explode('=', $value);
$type = $parts[1];
🌐
W3Resource
w3resource.com › php-exercises › php-array-exercise-37.php
PHP Array Exercise: Count the total number of times a specific value appears in an array - w3resource
<?php // Define a function to count occurrences of a value in an array function count_array_values($my_array, $match) { $count = 0; // Iterate through each element in the array foreach ($my_array as $key => $value) { // Check if the current ...
🌐
TutorialKart
tutorialkart.com › php › php-count-occurrences-of-specific-value-in-array
Count Occurrences of Specific Value in Array in PHP
May 8, 2023 - To count the number of occurrences of a specific value in given array in PHP, we can use count() and array_filter() functions. array_filter() function is used to filter only the elements that match given value, and then count() is used to find ...
🌐
W3Schools
w3schools.com › php › func_array_count_values.asp
PHP array_count_values() Function
PHP Examples PHP Compiler PHP Quiz PHP Exercises PHP Practice Problems PHP Server PHP Syllabus PHP Study Plan ... array() array_change_key_case() array_chunk() array_column() array_combine() array_count_values() array_diff() array_diff_assoc() array_diff_key() array_diff_uassoc() array_diff_ukey() array_fill() array_fill_keys() array_filter() array_flip() array_intersect() array_intersect_assoc() array_intersect_key() array_intersect_uassoc() array_intersect_ukey() array_key_exists() array_keys() array_map() array_merge() array_merge_recursive() array_multisort() array_pad() array_pop() array_
Find elsewhere
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-count-all-elements-in-an-array-in-php.php
How to Count All Elements or Values in an Array in PHP
You can simply use the PHP count() or sizeof() function to get the number of elements or values in an array. The count() and sizeof() function returns 0 for a variable that has been initialized with an empty array, but it may also return 0 for ...
🌐
StackHowTo
stackhowto.com › home › php › php – count array elements
PHP - Count Array Elements - StackHowTo
October 12, 2021 - You can use count() or sizeof() function to get the number of elements or values in an array. count() and sizeof() functions return 0 for an empty array, but can also return 0 for an undefined variable.
🌐
TutorialsPoint
tutorialspoint.com › array-count-values-function-in-php
PHP array_count_values() Function
July 30, 2019 - The array_count_values() function returns an associative array of values using the values of the input array as keys and their frequency in input array as values. It returns an associative array of values from input as keys and their count as value.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-array_count_values-function
PHP array_count_values() Function - GeeksforGeeks
June 20, 2023 - The array_count_values() is an inbuilt function in PHP which is used to count all the values inside an array. In other words we can say that array_count_values() function is used to calculate the frequency of all of the elements of an array. Syntax: array array_count_values( $array ) ...
🌐
Tutorialdeep
tutorialdeep.com › knowhow › php faqs › how to count occurrence of value in array in php
How to Count Occurrence of Value in Array in PHP
August 5, 2021 - In this tutorial, learn how to count occurrence of value in array using PHP. The short answer is to use the PHP array_count_values() Function of PHP that takes a single argument as an array. You can also find the number of times the specified element is present in an array.
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-count-all-array-elements-in-php
How to count all array elements in PHP ? - GeeksforGeeks
July 15, 2025 - Using iterator_count with ArrayIterator involves wrapping the array in an ArrayIterator object, which provides a way to traverse the array. The iterator_count function then counts the elements of this iterator, giving the total number of elements ...
🌐
Ui
php.kambing.ui.ac.id › manual › en › function.count.php
PHP: count - Manual
About 2d arrays, you have many way to count elements : <?php $MyArray = array ( array(1,2,3), 1, 'a', array('a','b','c','d') ); // All elements echo count($MyArray ,COUNT_RECURSIVE); // output 11 (9 values + 2 arrays) // First level elements echo count($MyArray ); // output 4 (2 values+ 2 arrays) // Both level values, but only values echo(array_sum(array_map('count',$MyArray ))); //output 9 (9 values) // Only second level values echo (count($MyArray ,COUNT_RECURSIVE)-count($MyArray )); //output 7 ((all elements) - (first elements)) ?>