$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$counts = array_count_values($array);
echo $counts['Ben'];
Answer from Rajat Singhal on Stack OverflowPHP
php.net โบ manual โบ en โบ function.array-count-values.php
PHP: array_count_values - Manual
array_count_values() returns an array using the values of array (which must be ints or strings) as keys and their frequency in array as values.
W3Schools
w3schools.com โบ Php โบ func_array_count_values.asp
PHP array_count_values() Function
The array_count_values() function counts all the values of an array.
10:26
Count the Occurrences of a Value in an Array | C Programming Example ...
04:32
8: How to count PHP array values - PHP 7 tutorial - YouTube
05:57
Array Count Values | PHP Array Function - YouTube
- YouTube
04:19
PHP Array Functions Bangla Tutorial Part-05 (array_count_values) ...
03:40
PHP Functions Video Tutorial: array_count_values - YouTube
Codecademy
codecademy.com โบ docs โบ php โบ arrays โบ array_count_values()
PHP | Arrays | array_count_values() | Codecademy
August 8, 2023 - The array_count_values() is a function that counts the occurrences of values in an array.
ZetCode
zetcode.com โบ php-array โบ array-count-values
PHP array_count_values - Counting Array Values in PHP
The PHP array_count_values function counts the occurrences of values in an array.
Jobtensor
jobtensor.com โบ Tutorial โบ PHP โบ en โบ Array-Functions-array_count_values
PHP Built-in array_count_values(), Definition, Syntax, Parameters, Examples | jobtensor
The array_count_values() function counts all the values of an array.
GeeksforGeeks
geeksforgeeks.org โบ php โบ what-is-the-use-of-array_count_values-function-in-php
What is the use of array_count_values() function in PHP ? - GeeksforGeeks
July 23, 2025 - The array_count_values() function is used to count all the values inside an array.
Top answer 1 of 10
146
$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$counts = array_count_values($array);
echo $counts['Ben'];
2 of 10
43
You can do this with array_keys and count.
$array = array("blue", "red", "green", "blue", "blue");
echo count(array_keys($array, "blue"));
Output:
3
Top answer 1 of 4
16
Using the variable $arr for your array, you could do this:
$out = array();
foreach ($arr as $key => $value){
foreach ($value as $key2 => $value2){
$index = $key2.'-'.$value2;
if (array_key_exists($index, $out)){
$out[$index]++;
} else {
$out[$index] = 1;
}
}
}
var_dump($out);
Output:
Array
(
[07/11-134] => 2
[07/11-145] => 2
[07/12-134] => 1
[07/12-99] => 1
)
Here's another version that produces it as a multidimensional array:
$out = array();
foreach ($arr as $key => $value){
foreach ($value as $key2 => $value2){
if (array_key_exists($key2, $out) && array_key_exists($value2, $out[$key2])){
$out[$key2][$value2]++;
} else {
$out[$key2][$value2] = 1;
}
}
}
Output:
Array
(
[07/11] => Array
(
[134] => 2
[145] => 2
)
[07/12] => Array
(
[134] => 1
[99] => 1
)
)
2 of 4
2
<?php
$array = array(array('07/11' => '134'), array('07/11' => '134'), array('07/12' => '145'));
$count = array();
foreach ($array as $val) {
foreach ($val as $key => $subval) {
$count[$key]++;
}
}
print_r($count);
Mc
sandbox.mc.edu โบ ~bennet โบ php โบ man โบ function.array-count-values.html
array_count_values
(PHP 4 >= 4.0.0)array_count_values -- Counts all the values of an array
Reintech
reintech.io โบ blog โบ phps-array-count-values-function-complete-guide
PHP's `array_count_values()` Function: A Complete Guide
April 14, 2023 - This function excels at transforming arrays into frequency distributions with minimal code. In scenarios ranging from text analysis to e-commerce product popularity tracking, understanding how to leverage array_count_values() effectively can significantly streamline your data processing workflows.
Tutorialspoint
tutorialspoint.com โบ php โบ php_function_array_count_values.htm
PHP array_count_values() Function
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.
Top answer 1 of 3
5
No such function exists. You always need to first map the values you would like to count (and those values must be string or integer):
$map = function($v) {return $v->name;};
$count = array_count_values(array_map($map, $data));
2 of 3
1
Since PHP 7.0, you can use array_column for this.
$counts = array_count_values(array_column($array_of_objects, 'name'));
When this question was originally asked and answered array_column didn't exist yet. It was introduced in PHP 5.5, but it couldn't handle arrays of objects until 7.0.
Lsu
ld2014.scusa.lsu.edu โบ php โบ function.array-count-values.html
Counts all the values of an array
array_count_values() returns an array using the values of array as keys and their frequency in array as values.
Top answer 1 of 3
2
You're on the right track, but you need to apply array_count_values() to each subarray, not the parent array.
$arr = [
'item1' => [1, 1, 3],
'item2' => [1, 1, 1, 2, 1],
'item3' => [1, 2, 2, 2, 3, 3, 3, 3, 3, 3]
];
$totals = array_map('array_count_values', $arr);
print_r($totals);
Results in
Array
(
[item1] => Array
(
[1] => 2
[3] => 1
)
[item2] => Array
(
[1] => 4
[2] => 1
)
[item3] => Array
(
[1] => 1
[2] => 3
[3] => 6
)
)
2 of 3
1
So, as @deceze advised - it is:
// applying `array_count_values` to each element of `$array`
print_r(array_map('array_count_values', $array));