How to find out the most common elements in an array using PHP?
In PHP, it is often necessary to find the elements that appear the most frequently in the array when processing an array. Although this function can be achieved using simple loops, using PHP built-in functions allows us to do this task more efficiently.
This article will introduce how to use some common functions in PHP to find the most frequently occurring elements in an array.
PHP provides a very convenient built-in function array_count_values , which can count the occurrences of each element in an array and return an associative array. A key is an array element, and a value is the number of times the element appears in the array.
Code example:
<?php
// Enter an array
$array = [1, 2, 3, 2, 1, 4, 2, 5, 1, 6, 3, 2];
// Count the number of occurrences of each element in the array
$count_values = array_count_values($array);
// Find the most frequently occurring elements
$max_value = max($count_values); // Find the maximum number of occurrences
$most_frequent = array_keys($count_values, $max_value); // Find all elements with maximum occurrences
echo "The most common element is:";
print_r($most_frequent);
?>
explain:
array_count_values($array) : Returns a new array containing the number of occurrences of each element.
max($count_values) : Find the number of times that appears the most.
array_keys($count_values, $max_value) : Get all elements with the maximum occurrence.
Output:
The most common element is:
Array
(
[0] => 2
)
In this example, the number 2 appears the most times.
If you not only need to find out the elements that appear the most, but also want to know their frequency, you can first use the array_count_values function to count the number, and then use the arsort function to sort the results in descending order.
Code example:
<?php
// Enter an array
$array = [1, 2, 3, 2, 1, 4, 2, 5, 1, 6, 3, 2];
// Count the number of occurrences of each element in the array
$count_values = array_count_values($array);
// Sort the statistics by the number of occurrences
arsort($count_values);
// Output sorted results
echo "Sorting of the number of occurrences of elements:\n";
print_r($count_values);
?>
Output:
Sorting of the number of occurrences of elements:
Array
(
[2] => 4
[1] => 3
[3] => 2
[4] => 1
[5] => 1
[6] => 1
)
explain:
arsort($count_values) : The statistical results are sorted in descending order by the number of occurrences.
The sorted array will show each element and its occurrences, with the most common elements ranked first.
In some cases, elements in an array are not just numbers, but strings or other types. If the array is larger, or you need to deal with more complex scenarios (such as removing certain elements or performing conditional filtering), you can extend the above code to meet specific needs.
By using PHP's built-in functions array_count_values and array , we can find out the most frequently occurring elements in the array very easily. These methods are not only efficient, but also can easily deal with different types of data, and are suitable for various practical development application scenarios.