Content · Programming · PHP

PHP array_count_values() Function: Usage and Examples

Learn how to use PHP array_count_values() to count repeated values, calculate frequencies, handle different data types, and summarize array data with practical examples.

4 views Published Oct 16, 2026 Updated Oct 16, 2026 Admin

When working with PHP arrays, you may need to find out how many times each value appears. For example, you might want to count survey responses, identify popular products, or summarize application statuses.

Instead of writing a loop to count every occurrence, PHP provides the array_count_values() function.

In this guide, you'll learn how it works, which values it supports, and how to use it in practical applications.

What Is array_count_values() in PHP?


The array_count_values() function counts how many times each distinct value appears in an array.

Its syntax is:

array_count_values(array $array): array

The function accepts one parameter:

  • $array — The array containing the values you want to count.

It returns an associative array where:

  • Each key represents a distinct value from the original array.
  • Each corresponding value contains the number of occurrences.

The function supports integer and string values. Other value types produce warnings and are skipped.

Example 1: Counting Repeated Strings


Let's count how many times each status appears in an array.

<?php

$statuses = [
    "open",
    "closed",
    "open",
    "pending",
    "closed",
    "open"
];

$result = array_count_values($statuses);

print_r($result);

Output:

Array
(
    [open] => 3
    [closed] => 2
    [pending] => 1
)

PHP automatically counts each distinct value and creates an associative array containing the results.

You can also retrieve the count for a specific value:

echo $result["open"];

Output:

3

This is useful when calculating how many records belong to each status or category.

Example 2: Counting Integer Values


The function also works with integers.

<?php

$ratings = [5, 4, 5, 3, 4, 5];

$counts = array_count_values($ratings);

print_r($counts);

Output:

Array
(
    [5] => 3
    [4] => 2
    [3] => 1
)

The result shows that rating 5 appears three times, rating 4 appears twice, and rating 3 appears once.

Notice that the function counts the array values, not the original array keys.

Does array_count_values() Distinguish Uppercase and Lowercase?


Yes. String values are case-sensitive.

<?php

$languages = ["PHP", "php", "PHP"];

print_r(array_count_values($languages));

Output:

Array
(
    [PHP] => 2
    [php] => 1
)

If you want case-insensitive counting for ASCII text, normalize the values first:

$normalized = array_map("strtolower", $languages);

$counts = array_count_values($normalized);

This produces a single php entry with a count of 3.

For multilingual text, consider appropriate Unicode-aware normalization instead.

Practical Example: Counting Survey Responses


Imagine you're developing a learning platform where visitors vote for the programming language they want to study next.

Each response contains a participant ID and their selected language.

We'll use array_column() to extract the selected languages and array_count_values() to count the votes.

Create a file named survey-results.php:

<?php

declare(strict_types=1);

// Survey responses.

$responses = [
    ["user_id" => 1, "language" => "PHP"],
    ["user_id" => 2, "language" => "Python"],
    ["user_id" => 3, "language" => "PHP"],
    ["user_id" => 4, "language" => "JavaScript"],
    ["user_id" => 5, "language" => "PHP"],
    ["user_id" => 6, "language" => "Python"]
];

// Extract the selected languages.

$languages = array_column(
    $responses,
    "language"
);

// Count votes for each language.

$results = array_count_values($languages);

// Sort by vote count, highest first.

arsort($results);

// Display the results.

echo "<h2>Survey Results</h2>";
echo "<ul>";

foreach ($results as $language => $votes) {

    echo "<li>" . htmlspecialchars(
        $language,
        ENT_QUOTES,
        "UTF-8"
    ) . ": " . $votes . " votes</li>";
}

echo "</ul>";

Browser output:

Survey Results

PHP: 3 votes
Python: 2 votes
JavaScript: 1 votes

The array_column() function extracts the languages, while array_count_values() calculates their frequencies.

Finally, arsort() sorts the counts from highest to lowest while preserving the language names as array keys.

This approach is useful for surveys, statistics, reporting, and other applications that need frequency summaries.

Handling Unsupported Value Types


The array_count_values() function only accepts integers and strings as elements.

Passing values such as booleans, floating-point numbers, arrays, or null causes PHP to generate an E_WARNING for each unsupported element.

Those elements are skipped rather than counted.

If your data comes from external sources, validate its types before processing it. When unsupported values are genuinely optional, you can filter them first:

<?php

$values = ["PHP", null, 5, true, "PHP"];

$validValues = array_filter(
    $values,
    fn($value): bool =>
        is_string($value) || is_int($value)
);

$counts = array_count_values($validValues);

print_r($counts);

Output:

Array
(
    [PHP] => 2
    [5] => 1
)

Filtering is appropriate only if ignoring those values matches your application's requirements. Otherwise, reject invalid data explicitly.

Also remember that integer values and equivalent numeric string keys can be combined because PHP converts certain numeric strings into integer array keys.

array_count_values() vs count() vs array_unique()


Function Purpose
array_count_values() Counts the occurrences of each distinct value.
count() Returns the total number of elements in an array.
array_unique() Removes duplicate values from an array.

Use array_count_values() when you need to know how often each value occurs, rather than simply counting elements or removing duplicates.

Common Mistakes


  • Expecting a single number: The function returns an associative array of frequency counts.
  • Using unsupported value types: Only integers and strings are supported.
  • Ignoring capitalization: "PHP" and "php" are counted separately.
  • Passing multidimensional arrays directly: Extract the values you want to count first.
  • Expecting empty arrays to produce counts: An empty input array returns an empty array.

Key Takeaways


  • array_count_values() counts the frequency of each distinct array value.
  • It returns an associative array containing values and their occurrence counts.
  • Only integer and string elements are supported.
  • String values are case-sensitive.
  • Use array_column() to extract fields before counting structured records.
  • Use arsort() to display the most frequent values first.
  • The original array remains unchanged.

The array_count_values() function provides a convenient way to summarize repeated values in PHP. It is especially useful for generating statistics, counting survey responses, analyzing categories, and building simple reports.

For more information, see the official PHP array_count_values() documentation.

Share this resourceSend this page to someone who may find it useful.