Current Location: Home> Latest Articles> How to count the number of times each value appears in an array?

How to count the number of times each value appears in an array?

M66 2025-05-12

In PHP programming, it is a common requirement to count the number of times each value appears in an array. Whether in data analysis, log statistics, or in various programs, this function can help us process array data effectively. This article will introduce several different implementation methods to help you easily master how to complete this task in PHP.

Method 1: Use array_count_values() function

The built-in array_count_values() function of PHP is the most direct and easiest way to deal with this problem. It takes an array as a parameter and returns an associative array, the key is the value in the array, and the value is the number of times the value appears.

Sample code:

 <?php
$array = ["apple", "banana", "apple", "orange", "banana", "banana"];
$result = array_count_values($array);

print_r($result);
?>

Output result:

 Array
(
    [apple] => 2
    [banana] => 3
    [orange] => 1
)

As shown above, array_count_values() automatically counts the number of occurrences of each value in the array.

Method 2: Use foreach loop and manual counting

If you want to customize each element in the array, or in some cases require more flexibility, you can use a foreach loop to manually count the number of occurrences of each value.

Sample code:

 <?php
$array = ["apple", "banana", "apple", "orange", "banana", "banana"];
$count = [];

foreach ($array as $value) {
    if (isset($count[$value])) {
        $count[$value]++;
    } else {
        $count[$value] = 1;
    }
}

print_r($count);
?>

Output result:

 Array
(
    [apple] => 2
    [banana] => 3
    [orange] => 1
)

This approach is suitable for scenarios where you need to customize more logic, for example, if you want to do other operations on the count, or just counting values ​​of a specific type.

Method 3: Use array_reduce() function

array_reduce() is a functional programming method that can collapse an array into a single value through a callback function. Here, we can use it to implement the function of value counting.

Sample code:

 <?php
$array = ["apple", "banana", "apple", "orange", "banana", "banana"];

$count = array_reduce($array, function($carry, $item) {
    if (isset($carry[$item])) {
        $carry[$item]++;
    } else {
        $carry[$item] = 1;
    }
    return $carry;
}, []);

print_r($count);
?>

Output result:

 Array
(
    [apple] => 2
    [banana] => 3
    [orange] => 1
)

This approach is very effective when dealing with more complex array operations, especially when more complex aggregation logic is required.

Method 4: Use array_walk() and reference

array_walk() allows you to modify each element of the array. If you need to modify the data in the array through references, combined with other methods, you can also count the number of occurrences of each element.

Sample code:

 <?php
$array = ["apple", "banana", "apple", "orange", "banana", "banana"];
$count = [];

array_walk($array, function($item) use (&$count) {
    if (isset($count[$item])) {
        $count[$item]++;
    } else {
        $count[$item] = 1;
    }
});

print_r($count);
?>

Output result:

 Array
(
    [apple] => 2
    [banana] => 3
    [orange] => 1
)

This method is similar to foreach , but modify the array by reference so that the count can be directly updated to external variables.

Summarize

The above methods show how to use different techniques and built-in functions to count the number of occurrences of each value in an array. The simplest way is to use array_count_values() , but if you need more flexibility and control, you can choose to loop manually or use functional programming.

Choosing a method that suits your needs can help you process array data more efficiently, improving the readability and execution efficiency of your code.