PHP array_count_values() function


The array_count_values() function in PHP is used to count the frequency of all the values in an array. It returns an associative array where the keys are the unique values from the input array, and the corresponding values are the number of times each unique value appears.

Syntax:

array_count_values(array $array): array

Parameters:

  • $array: The input array whose values you want to count. The array should be a simple one-dimensional array (indexed or associative), and the values can be of any data type that can be treated as keys (e.g., integers, strings).

Return Value:

  • The function returns an associative array where the keys are the unique values of the input array, and the values are the counts (frequencies) of how many times each unique value appears in the input array.

Example 1: Basic Usage

<?php $array = ["apple", "banana", "apple", "orange", "banana", "apple"]; // Count the occurrences of each value $counted = array_count_values($array); print_r($counted); ?>

Output:

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

In this example, the function counts how many times each fruit appears in the array.

Example 2: Using Numbers as Values

The array_count_values() function also works with arrays containing numbers.

<?php $numbers = [1, 2, 2, 3, 3, 3, 4]; // Count the occurrences of each number $counted = array_count_values($numbers); print_r($counted); ?>

Output:

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

In this case, the function counts how many times each number appears in the array.

Example 3: Handling Mixed Data Types

If the array contains mixed data types, the array_count_values() function will treat strings and numbers as different values.

<?php $mix = [1, "1", 2, "apple", "apple", 3]; // Count the occurrences $counted = array_count_values($mix); print_r($counted); ?>

Output:

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

In this example, the number 1 and the string "1" are treated as separate values.

Example 4: Empty Array

If you pass an empty array to array_count_values(), it will return an empty array.

<?php $empty_array = []; // Count occurrences in an empty array $counted = array_count_values($empty_array); print_r($counted); ?>

Output:

Array ( )

Practical Usage:

  • array_count_values() is useful for:
    • Counting the frequency of elements in datasets, such as survey results or data logs.
    • Analyzing data to find trends, such as which items appear most frequently.
    • Cleaning up datasets by identifying duplicate values or repetitive entries.

Limitations:

  • This function only works on simple, one-dimensional arrays. If you pass a multi-dimensional array, array_count_values() will not work as expected.
  • It treats data types strictly, meaning numeric strings and numbers are considered different values.

Summary:

  • array_count_values($array) counts the frequency of values in a one-dimensional array and returns an associative array where the keys are the unique values, and the values are the frequencies.
  • It’s useful for counting how many times each element appears in an array, whether they are strings, numbers, or other data types.