$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
array_count_values(array) ❮ PHP Array Reference · ★ +1 · Sign in to track progress · REMOVE ADS · PLUS · SPACES · GET CERTIFIED · FOR TEACHERS · BOOTCAMPS · CONTACT US · × · If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ·
04:32
8: How to count PHP array values - PHP 7 tutorial - YouTube
05:57
Array Count Values | PHP Array Function - YouTube
08:36
10: How count PHP array elements - PHP 7 tutorial - YouTube
07:38
Array count & Size in PHP | Tutorial 21 - YouTube
03:40
PHP Functions Video Tutorial: array_count_values - YouTube
05:46
29 - PHP Array Function - Count Array Values - Repeated Values ...
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.
Reintech
reintech.io › blog › phps-array-count-values-function-complete-guide
PHP's `array_count_values()` Function: A Complete Guide
April 14, 2023 - The array_count_values() function accepts an array as input and returns an associative array where keys represent unique values from the input, and values represent their occurrence counts.
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
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.
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.
Top answer 1 of 5
58
function array_count_values_of($value, $array) {
$counts = array_count_values($array);
return $counts[$value];
}
Not native, but come on, it's simple enough. ;-)
Alternatively:
echo count(array_filter($array, function ($n) { return $n == 6; }));
Or:
echo array_reduce($array, function (
n) { return
n == 6); }, 0);
Or:
echo count(array_keys($array, 6));
2 of 5
14
This solution may be near to your requirement
$array = array(1, 2, 3, 3, 3, 4, 4, 5, 6, 6, 6, 6, 7);
print_r(array_count_values($array));
Result:
Array
( [1] => 1 ,[2] => 1 , [3] => 3, [4] => 2,[5] =>1, [6] => 4, [7] => 1 )
for details.
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. ... <?php $cities = array("New York", "Berlin", "Tokyo", "Berlin", "Berlin", "Tokyo"); print_r(array_count_values($cities));
W3Schools
w3schools.com › php › func_array_count.asp
PHP count() Function
AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll ... 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(
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.
Nusphere
nusphere.com › kb › phpmanual › function.array-count-values.htm
PHP Manual: array_count_values
PhpED - PHP IDE integrated development environment for developing web sites using PHP, HTML, Perl, JScript and CSS that combines a comfortable editor, debugger, profiler with the MySQl, PostrgeSQL database support based on easy wizards and tutorials.Easy to use for debugging PHP scripts, publishing ...
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
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 a variable that isn't set. You can additionally use the isset() function to check whether a variable is set or not. ... <?php $days = array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); // Printing array size echo count($days); echo "<br>"; echo sizeof($days); ?>
Matt Doyle
elated.com › home › blog › counting php array elements using count()
Counting PHP Array Elements Using count()
July 23, 2022 - It then loops through each element index, from 0 through to $totalElements - 1 (i.e. 3), displaying the element’s value as it goes. (The if statement displays a comma and space after each element except for the last.) You do need to be bit careful when using count() with for. As you saw in Creating Arrays in PHP, PHP doesn’t distinguish between indexed and associative arrays, and numeric array indices don’t have to be contiguous either.