PHP array_count_values: How it Works with Examples

php array count values

Developers use the array_count_values function in PHP to find how many times each value appears in an array.

What is the array_count_values Function in PHP?

The array_count_values function in PHP counts how many times a value exists inside an array. It works only with values that can act as keys. It does not count objects or arrays because keys must be scalar values.

The syntax looks like this:

array_count_values(array $array): array

It accepts one parameter, which is the array. It returns an array where keys are unique values from the input and values are the count of how many times they appear.

Here is a quick example:

$nums = [1, 2, 2, 3, 3, 3];
print_r(array_count_values($nums));

The output:

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

This example shows you:

  • One appears once.
  • Two appear twice.
  • Three appear three times.

Here is another example with a mixed array:

$data = ["apple", "apple", 10, 10, 10, "banana"];
print_r(array_count_values($data));

The output:

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

This result shows that the function can work with both string values and numbers.

The Differences Between array_count_values and count in PHP

The count function in PHP tells you how many elements exist in an array. The array_count_values function tells you how many times each unique value repeats.

Featurecountarray_count_values
PurposeNumber of elementsCount of each value
ReturnIntegerArray
InputArrayArray

Use count when you only want the size of the array. Use array_count_values when you want to group values and know how often each one repeats.

Examples

Count Values in a Simple Array:

$letters = ["a", "b", "a", "c", "b", "a"];
print_r(array_count_values($letters));

The output:

Array
(
[a] => 3
[b] => 2
[c] => 1
)

This example shows how the function counts repeated letters inside a simple array and outputs the number of times each one appears.

Count Mixed Numbers and Strings:

$data = [1, "1", 1, 2, "apple", "apple", 2];
print_r(array_count_values($data));

The output:

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

In this example the function counts numbers and strings. It treats one and “1” as two different keys, and it shows how string values can repeat like numbers.

Use with Large Array:

$items = array_merge(range(1, 5), [3, 3, 4, 4, 4, 5, 5, 5, 5]);
print_r(array_count_values($items));

The output:

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

Here, the function counts values in a larger array that contains a range of numbers and extra repeated values, so the output highlights how counts grow with more data.

Count Results then Filter:

$data = ["apple", "apple", "banana", "banana", "banana", "orange"];
$counts = array_count_values($data);
foreach ($counts as $fruit => $count) {
    if ($count > 2) {
        echo $fruit . " repeats " . $count . " times\n";
    }
}

The output:

banana repeats 3 times

This example shows how you can use the result of array_count_values inside a loop. It prints only the items that repeat more than two times.

Wrapping Up

You learned how the array_count_values function works and how it differs from count. You also saw how to use it with mixed values and advanced cases.

Here is a quick recap:

  • array_count_values returns counts of each value in an array.
  • count returns only the size of the array.
  • Use array_count_values when you need to group and count values.

FAQs

What does PHP array_count_values do with an array?

The array_count_values() function in PHP counts how many times each value appears in an array. It returns an associative array.
$nums = array(1, 2, 2, 3, 3, 3);
$result = array_count_values($nums);
print_r($result);
Output: Array ( [1] => 1 [2] => 2 [3] => 3 )

How can I use array_count_values with strings in PHP?

You can apply array_count_values() on an array of strings to count repeated words or names.
$words = array("apple","banana","apple","orange","banana","apple");
$result = array_count_values($words);
print_r($result);
Output: Array ( [apple] => 3 [banana] => 2 [orange] => 1 )

What is the difference between count and array_count_values?

  • count() returns the total number of elements in an array.
  • array_count_values() counts how many times each value appears.
$items = array("pen","pen","book","pencil");
echo count($items); 
// Output: 4

print_r(array_count_values($items)); 
// Output: Array ( [pen] => 2 [book] => 1 [pencil] => 1 )

Similar Reads

PHP $_COOKIE: Securely Store and Manage Data

Ever notice how some websites just seem to "know" you? That’s thanks to cookies! When you’re working with PHP, $_COOKIE becomes a…

PHP Ternary Operator: How to Write Short Expressions

The PHP ternary operator gives you a way to write short expressions. It reduces lines of code and avoids long…

PHP Hello World: Write Your First Program with Code Example

You always start with “Hello World” when you learn a new language. It keeps things simple. You see right away…

PHP cURL: HTTP Requests and Responses in PHP

In PHP, there are plenty of times when you need to connect with other servers or APIs. That's where the…

Static Method in PHP: How They Work in Classes

PHP static method lets you call functions without an object. In this article, we will cover the following topics: The…

PHP Access Modifiers: How Public, Private & Protected Work

PHP supports OOP with access modifiers to prevent unintended changes and improve security. In this article, we will cover the…

PHP OOP Constructor: How It Works in a Class with Examples

The OOP constructor initializes an object when it is created in PHP. Understand What a Constructor Is in PHP A…

How to Create Table in MySQL with PHP?

One of the first tasks you will likely encounter is creating a table in databases. Tables are the main part…

PHP Static Property: How It Works & Examples

PHP static property helps you manage shared data across instances, but can lead to hidden state changes. In this article,…

PHP range Function: Create Arrays with Sequence Values

The PHP range function helps you make sequences. Without it, you would write loops every time you need numbers or…

Previous Article

JavaScript Polyfilling & Transpiling Guide for Beginners

Next Article

JavaScript Ninja Code for Beginners

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.