PHP array_count_values() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_count_values() counts how many times each value appears in an array. It returns a new array where the keys are the original values and the values are the counts.

You use array_count_values() for frequency tasks such as counting votes, repeated words, tags or status codes. It replaces a manual loop that adds 1 to a counter for every item.

Syntax

Call array_count_values() with the values you need. Start with this line.

PHP
$counts = array_count_values($words);
CallWhat it does
array_count_values(['a', 'b', 'a'])Returns ['a' => 2, 'b' => 1].
array_count_values([1, 1, 2])Returns [1 => 2, 2 => 1], with the numbers as keys.
array_count_values([])Returns an empty array.

Only integers and strings can be counted. The order of the result follows the first time each value appears in the input. Use arsort() on the result when you want the most common value first.

Library declarations
PHP
array_count_values(array $array): array

Parameters

ParameterDescription
arrayRequired. The array whose values should be counted. The values must be integers or strings.

Return Value

Return ValueDescription
arrayAn associative array of value => count, in order of first appearance.

PHP Examples

Example 1 — Counting Repeated Values

Count how many times each fruit appears in a list.

PHP
<?php
$fruits = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple'];

print_r(array_count_values($fruits));

How It Works

  1. The list has six fruits, and some of them repeat.
  2. array_count_values() goes through the list and counts each distinct value.
  3. 'apple' appears three times, 'banana' twice and 'cherry' once.
  4. The result keeps the order in which each fruit first appeared.

Example 2 — Numbers and Numeric Strings

The integer 1 and the string '1' are the same array key, so they are counted together. Letters are case sensitive.

PHP
<?php
$values = [1, '1', 2, 'a', 'A'];

print_r(array_count_values($values));

How It Works

  1. The value '1' is converted to the integer key 1, so 1 and '1' share one counter.
  2. That counter reaches 2.
  3. The value 2 appears once.
  4. 'a' and 'A' are different strings, so each one is counted once.

Example 3 — Word Frequency, Most Common First

Split a sentence into words, count them and sort the counts from high to low.

PHP
<?php
$text = 'the cat and the hat and the bat';
$counts = array_count_values(explode(' ', $text));

arsort($counts);

foreach ($counts as $word => $n) {
    echo "$word: $n", PHP_EOL;
}

How It Works

  1. explode(' ', $text) turns the sentence into a list of words.
  2. array_count_values() counts each word.
  3. arsort() sorts the counts from the highest to the lowest and keeps the keys.
  4. Words with the same count keep their original order, because PHP sorting is stable since version 8. 0.

Frequently Asked Questions

It counts how often each value occurs in an array and returns an array of value => count pairs.
Only strings and integers. Other types such as floats, booleans and null are skipped and PHP shows a warning for each skipped value.
Yes. 'PHP' and 'php' are counted as two different values. Lowercase the items first, for example with array_map('strtolower', $items), when you want a case insensitive count.
Call arsort() on the result so the highest count comes first, then use array_key_first() to read the most common value.
count() returns the total number of items in an array. array_count_values() returns how many times each distinct value appears.

Next: array_diff()

array_diff() returns values from the first array that are missing from the other arrays.

array_diff() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful