Current Location: Home> Latest Articles> Use array_count_values() to handle simple arrays of numbers

Use array_count_values() to handle simple arrays of numbers

M66 2025-06-07

In PHP, the array_count_values() function is a very useful tool that can count the number of occurrences of individual values ​​in an array. Whether it is a string array or an array of numbers, array_count_values() can help you quickly get the frequency of each element's appearance. This is very convenient for analyzing data, generating statistical information, or processing simple data analysis tasks.

1. Introduction to array_count_values() function

array_count_values() is a built-in PHP function that takes an array as input and returns an associative array where the key is the element in the input array and the value is the number of times the element appears in the original array. This function works with arrays of all types, but it is usually used to handle arrays of numbers or strings.

grammar:

 array_count_values(array $array): array
  • $array : This is the array to count.

The return value is an associative array where each value in the array is used as a key and the number of occurrences is used as a value.

2. Sample code: Statistics the number of occurrences of each element in the array of numbers

Here is a simple example showing how to use array_count_values() to count the occurrence of each element in an array of numbers:

 <?php
// Define an array of numbers
$numbers = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4];

// use array_count_values() Function statistics number of elements occur
$result = array_count_values($numbers);

// Output result
print_r($result);
?>

Output result:

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

As shown above, array_count_values() returns an associative array where the key is the element in the array and the value is the number of occurrences of that element. In our example, the number 1 appears 1, the number 2 appears 2, and so on.

3. Application in actual development

The array_count_values() function can not only be used to count elements in an array of numbers, but also to other types of arrays. For example, if you have an array containing multiple strings, you can also use this function to count the number of occurrences of each string. Here is an example of processing string arrays:

 <?php
// Define an array of strings
$fruits = ["apple", "banana", "apple", "orange", "banana", "apple"];

// use array_count_values() Function statistics number of elements occur
$result = array_count_values($fruits);

// Output result
print_r($result);
?>

Output result:

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

In this example, we count the number of occurrences of each element in the fruit name array.

4. Process URL array

Suppose you have an array containing URLs and want to count the number of occurrences of each domain name or path. array_count_values() can also help you achieve this. Let's give an example:

 <?php
// Define a containing URL Array of
$urls = [
    "https://example.com/page1",
    "https://m66.net/page2",
    "https://example.com/page3",
    "https://m66.net/page1",
    "https://m66.net/page3",
    "https://m66.net/page2"
];

// extract URL Domain name part and count the number of occurrences
$domains = array_map(function($url) {
    return parse_url($url, PHP_URL_HOST);
}, $urls);

// use array_count_values() Statistics the number of occurrences of domain names
$result = array_count_values($domains);

// Output result
print_r($result);
?>

Output result:

 Array
(
    [example.com] => 2
    [m66.net] => 4
)

In this example, we used the parse_url() function to extract the domain name part from the URL, and then used array_count_values() to count the number of occurrences of each domain name. You can see that m66.net appears more times.

5. Summary

array_count_values() is a very practical PHP function that is suitable for quickly counting the occurrence of each element in an array. Whether it is an array of numbers, strings, or an array containing URLs, it can provide you with a simple solution.

If you want to handle more complex array statistics tasks, you can also combine other PHP functions (such as parse_url() ) to extract the data and analyze it further. Hope this article helps you better understand and use the array_count_values() function.