Current Location: Home> Latest Articles> Basic syntax and examples of array_count_values() function

Basic syntax and examples of array_count_values() function

M66 2025-05-10

In PHP, the array_count_values() function is a very practical tool that can help us count the number of occurrences of each element in an array. This article will help you understand how to use this function through detailed syntax introduction and examples.

1. Function syntax

 array_count_values(array $array): array
  • $array : an array of inputs containing data that you want to count the frequency of elements.

  • Return value : Returns an associative array where the key of the array is the element in the original array and the value is the number of occurrences of the corresponding element.

2. How to use array_count_values() to perform element frequency statistics

Suppose you have an array containing multiple elements, the array_count_values() function can directly return a new array, indicating the number of times each element appears in the original array. Here are some examples.

Example 1: Statistics on element frequency of simple arrays

 <?php
$array = ["apple", "banana", "apple", "orange", "banana", "apple"];

$result = array_count_values($array);
print_r($result);
?>

Output result:

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

In this example, array_count_values() counts the number of occurrences of each fruit in the array. Apple appeared 3 times, banana appeared 2 times, and orange only appeared 1 time.

Example 2: Statistical element frequency of array of numbers

 <?php
$array = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4];

$result = array_count_values($array);
print_r($result);
?>

Output result:

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

In this example, array_count_values() counts the frequency at which each number appears in the numeric array.

Example 3: Statistics arrays containing different data types

 <?php
$array = [1, 2, "apple", "banana", "apple", 2, 3, "banana"];

$result = array_count_values($array);
print_r($result);
?>

Output result:

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

In this example, the array contains numeric and string elements, and array_count_values() can still correctly count the number of occurrences of each element.

III. Application scenarios of array_count_values() function

  1. Statistical data occurrence frequency : This function is especially suitable for scenarios where the number of occurrences of each element in the statistics is required. For example, when analyzing user behavior data, it may be necessary to know how often a particular operation or event occurs.

  2. Text analysis : You can use array_count_values() to analyze the word frequency in the text. For example, you can first split the text into a word array, and then use array_count_values() to count the number of times each word appears.

  3. Deduplication operation : By counting element frequency, you can easily implement array deduplication. Just get the element with a frequency of 1.

4. Things to note

  1. Types of array keys : array_count_values() will count the elements of the array as keys, so the keys in the array must be of comparable types (for example, integers, strings). If there is an illegal type (for example, a resource), it will cause an error.

  2. Key Order : The order of key values ​​in the returned array is determined by the sorting mechanism inside PHP. If you want to sort the statistical results, you can use functions such as asort() .

  3. Performance considerations : For very large arrays, array_count_values() may cause large memory consumption, so special attention should be paid when handling big data.

5. Summary

array_count_values() is a very powerful function that counts the frequency of occurrence of each element in an array. Whether it is used for data analysis, text processing, or simple array operations, this function can provide you with great convenience. Through the example above, we can see how to use array_count_values() to process different types of data in a practical application.

I hope this article can help you better understand and apply the array_count_values() function of PHP!