array_count_values

Counts how many times each distinct value occurs in an array and returns value => count pairs.

Signature
array_count_values(array $array): array
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$arrayarrayThe array of values to count. Elements should be int or string.

Return value

arrayAn associative array using the input's values as keys and their number of occurrences as values.

Errors

Raises an E_WARNING for every element that is not an int or a string; those elements are skipped rather than counted.

array_count_values() builds a frequency table: every distinct value in the input becomes a key of the result, and the value under that key is how many times it appeared. It is the one-call answer to "how many of each?".

When to reach for it

Combine it with array_column() to summarise a result set — pull the status column out of 5,000 rows and count them all in two calls. It is also the quick way to find duplicates (array_filter() the result for counts greater than one), to build a word-frequency list after explode(), or to produce the data behind a small bar chart. Follow it with arsort() to get a ranked "top values" list.

What trips people up

Only integers and strings can be counted, because only those can be array keys. Floats, booleans, nulls, arrays and objects each raise a warning and are skipped silently in the output — the totals will not add up to count($input).

Numeric strings are also normalised by PHP's key rules, so "5" and 5 are counted together as one entry. If that distinction matters, cast everything to string first with array_map('strval', $values).

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Summarise how many orders sit in each status after fetching a result set
  • Find duplicate email addresses in an import file before inserting
  • Build a word-frequency list for a simple tag cloud or search-term report

Frequently asked questions

Why does array_count_values warn about my array?

It can only count values that are valid array keys, meaning integers and strings. Any float, boolean, null, array or object triggers an E_WARNING and is left out of the result.

Does array_count_values treat '5' and 5 as the same value?

Yes. Numeric strings become integer array keys, so the string '5' and the integer 5 are tallied under one key. Cast everything with array_map('strval', $array) first if you need them separate.

How do I get the most frequent value in an array?

Pass the array through array_count_values(), sort the result with arsort(), then read array_key_first(). That gives you the value with the highest count.

More examples

Finding duplicates in a list

Counting first and filtering for counts above one gives both the duplicated values and how often each appeared — information array_unique() throws away.

Example · php
<?php
$emails = ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]'];

$dupes = array_filter(array_count_values($emails), fn($n) => $n > 1);
print_r($dupes);
echo "duplicate addresses: ", count($dupes), "\n";

Summarising rows by a column

array_column() flattens the status field out of every row and array_count_values() tallies it, producing ['paid' => 2, 'pending' => 1] without a single loop.

Example · php
<?php
$orders = [
    ['id' => 1, 'status' => 'paid'],
    ['id' => 2, 'status' => 'pending'],
    ['id' => 3, 'status' => 'paid'],
];

print_r(array_count_values(array_column($orders, 'status')));

Non-string values are skipped

The integer 1 and the string '1' land on the same key and are counted together as 3, showing PHP's key normalisation at work. Adding a float or null to the input would raise a warning and drop that element entirely.

Example · php
<?php
$mixed = ['a', 'a', 1, 1, '1'];

$counts = @array_count_values($mixed);
var_dump($counts);
echo "input: ", count($mixed), " counted: ", array_sum($counts), "\n";

Discussion

  • Be the first to comment on this lesson.