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

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

M66 2025-06-07

How to count the number of times a specific value appears in an array in PHP? Is there any easy way?

In PHP, if you need to count the number of times a specific value appears in an array, there are several simple and commonly used methods. Today we will introduce several common implementation methods to help you complete this task more efficiently.

Method 1: Use array_count_values() function

The built-in array_count_values() function in PHP is very convenient, and can count the number of times each value appears in the array. Returns an associative array where the key is the value in the array and the value is the number of times the value appears.

Sample code:

 <?php
// Example array
$array = array("apple", "banana", "apple", "orange", "apple", "banana");

// use array_count_values() Count the number of times each value appears
$counted = array_count_values($array);

// Output result
echo "apple Appeared " . $counted['apple'] . " Second-rate。<br>";
echo "banana Appeared " . $counted['banana'] . " Second-rate。<br>";
echo "orange Appeared " . $counted['orange'] . " Second-rate。<br>";
?>

explain:

  1. array_count_values() returns a new array containing each value and the number of times it appears.

  2. By accessing a specific key in this array, we can easily get the number of occurrences of a specific value.

Method 2: Use array_filter() and count() functions

If you want to have more flexibility in controlling the statistical process, using array_filter() and count() combinations is also a good choice. array_filter() can filter arrays according to given conditions, and then calculate the length of the filtered array through the count() function.

Sample code:

 <?php
// Example array
$array = array("apple", "banana", "apple", "orange", "apple", "banana");

// Target value
$target = "apple";

// use array_filter() Filter arrays,找出所有Target value
$filteredArray = array_filter($array, function($value) use ($target) {
    return $value === $target;
});

// 统计Target value出现的Second-rate数
echo "$target Appeared " . count($filteredArray) . " Second-rate。<br>";
?>

explain:

  1. array_filter() returns a new array based on the condition, containing all elements equal to $target .

  2. The count() function returns the length of the filtered array, which is actually the number of times the target value appears in the original array.

Method 3: Use foreach loop to manually count

If you want to fully customize the statistics process, or don't want to use built-in functions, you can also manually count the number of occurrences of a specific value in the array through a foreach loop.

Sample code:

 <?php
// Example array
$array = array("apple", "banana", "apple", "orange", "apple", "banana");

// Target value
$target = "apple";

// Initialize the counter
$count = 0;

// use foreach Loop through the array
foreach ($array as $value) {
    if ($value === $target) {
        $count++;
    }
}

// Output result
echo "$target Appeared $count Second-rate。<br>";
?>

explain:

  1. Iterate through the entire array through foreach loop.

  2. Whenever the target value $target is encountered, the counter $count is increased.

  3. Ultimately, $count is the number of times the target value appears.

Summarize

The above are three common methods for the occurrence of specific values ​​in PHP:

  1. Using array_count_values() is the easiest and straightforward way.

  2. Using array_filter() and count() can provide more flexibility.

  3. Manual statistics using foreach loops are suitable for more complex needs.

Depending on your specific needs, you can choose the right method. If you have high performance requirements, it is recommended to use array_count_values() because it is an efficient method built in PHP. Hope this article helps you!


Non-text part: