Introduction
In this article, we will see how to get count of specific values in a PHP array
. Often, we need to count the occurrence of particular values in an array. In other words, to find the frequency of values in an array. Data analysis problems usually involve finding frequencies, where you might be investigating a dataset to answer a “HOW MANY?” type of question.

We have already seen how to count values in a multidimensional array, but here, we will explore an approach to get total count of a specific value in an array that could be one or more than one dimensional.
Programming languages have specialized utilities, and so does PHP. We will explore PHP array_count_values
function that count specific values in array PHP. So, let’s jump straight to the article.
Anatomy of array_count_values
Description
This function counts all the values of an array
Function Signature
array_count_values(array $array): array
Arguments
- array – The input array
Return Type
Returns an associative array with values of the input array as keys and their counts as values.
Usage Example
<?php
$responses = ["Yes","No","No","Yes","Yes","No","Yes","Yes","No","No","Yes","Yes","Yes","No","Yes","Yes","Yes","No"];
//Counts the number of "Yes" and "No"
$responses_count = array_count_values($responses);
echo "Total count of positive responses (Yes) is: ".$responses_count["Yes"].PHP_EOL;
echo "Total count of negative responses (No) is: ".$responses_count["No"].PHP_EOL;
//OUTPUT
//Total count of positive responses (Yes) is: 11
//Total count of negative responses (No) is: 7
?>
Example#1 – Get count of specific values in a PHP array by using array_count_values
In the usage example of the function array_count_values
, we have seen how the function works. However, if we have a multidimensional array, then the function might not work as expected. So, we have to use another function to assist array_count_values to count specific values in array PHP.
We have an array containing records about students and the elective they have opted for. We want to count the frequency of the subjects.
$students_electives = [
"Andy" => ["ID"=>"1001", "Elective"=>"Computer Science"],
"Benjamin" => ["ID"=>"1002", "Elective"=>"Electronics"],
"Catheline" => ["ID"=>"1003", "Elective"=>"Economics"],
"Dexter" => ["ID"=>"1004", "Elective"=>"Computer Science"],
"Eion" => ["ID"=>"1004", "Elective"=>"Computer Science"],
"Franklin" => ["ID"=>"1005", "Elective"=>"Mathematics"],
"Gina" => ["ID"=>"1006", "Elective"=>"Chemistry"],
"Hudson" => ["ID"=>"1007", "Elective"=>"Electronics"],
"Ira" => ["ID"=>"1008","Elective"=>"Mathematics"],
"Jessica" => ["ID"=>"1009","Elective"=>"Economics"],
"Kelvin" => ["ID"=>"1010","Elective"=>"Computer Science"],
"Liam" => ["ID"=>"1011","Elective"=>"Mathematics"],
];
Here’s a way to use array_count_values to get the count of electives conveniently.
$electives = array_column($students_electives, 'Elective');
//Counts the number of "Yes" and "No"
$electives_count = array_count_values($electives);
print_r($electives_count);
/*
OUTPUT
Array
(
[Computer Science] => 4
[Electronics] => 2
[Economics] => 2
[Mathematics] => 3
[Chemistry] => 1
)
*/
Voila! We have used the array_column function to isolate the Electives column and then passed that column to get total count of specific values in a PHP array. That’s the most convenient way while using th array_count_values function. There’s also a recursive approach to get the count of the array at the lowest levels of the array or in other words, at the leaves of an array.
Example#2 – Count specific values in a PHP array by recursion
PHP array_walk_recursive
function is ideal for digging deep into a multidimensional array and accessing the nodes at the leaves. We use this function together with array_count_values to access the electives that reside at the resulting array’s leaves. Here’s how.
$resultant = [];
array_walk_recursive($students_electives,function($value,$key) use (&$resultant){
if(!is_numeric($value)) {
//Initiate the count to one for the newly created subject.
if(!isset($resultant[$value])) {
$resultant[$value] = 1;
}
//If a subject reoccurs then increase the count by one.
else {
$resultant[$value] += 1;
}
}
},$resultant);
print_r($resultant);
/*
OUTPUT
Array
(
[Computer Science] => 4
[Electronics] => 2
[Economics] => 2
[Mathematics] => 3
[Chemistry] => 1
)
*/
Great! We have found out the count of specific values in a PHP array recursively. Next, let’s see an iterative approach to do the same operation. There’s always more than one approach, and the situation at which we have to choose a specific operation decides its feasibility.
Example#3 – Get count of specific values in a PHP array by iteration
Iteration
involves looping through an array to perform a specific operation. We will loop through the array to get the sum. However, the iterative approach involves nested loops, and the more the nested loops, the lesser the efficiency of code.
$resultant = [];
foreach($students_electives as $student=>$elective) {
//Check if a key has been created for the subject
if(!isset($resultant[$elective["Elective"]])) {
//Initiate the count to one for the newly created subject.
$resultant[$elective["Elective"]] = 1;
}
else {
//If a subject reoccurs then increase the count by one.
$resultant[$elective["Elective"]] += 1;
}
}
print_r($resultant);
/*
OUTPUT
Array
(
[Computer Science] => 4
[Electronics] => 2
[Economics] => 2
[Mathematics] => 3
[Chemistry] => 1
)
*/
We have used one loop to access the array and then used direct indexing to get the Elective. Alternatively, we could have used nested loops, one to access the students_electives array and another to access each student’s array.
Conclusion
This article explores how to count specific values in array PHP. We have seen three different approaches, by using array_count_values
function, recursive
, and iterative
approach. Three of these approaches elegantly solve the problem at hand. We hope you’ve learned something new. Stay tuned for more exciting articles related to PHP.