In PHP, array_unique() is a very common function to remove duplicate values from an array. However, in many practical application scenarios, we may need to count the frequency of occurrence of each element in the array before deduplication. Below, we will show how to combine array_unique() to count the frequency of elements in the array before deduplication.
First, we need to use the array_count_values() function provided by PHP to count the frequency of each element in the array. The function returns an associative array where the key is the element in the original array and the value is the number of times the element appears in the array.
<?php
// Original array
$array = ["apple", "banana", "apple", "orange", "banana", "apple"];
// Statistical frequency
$frequencies = array_count_values($array);
// Output frequency statistics results
print_r($frequencies);
?>
Output result:
Array
(
[apple] => 3
[banana] => 2
[orange] => 1
)
Through array_count_values() , we can easily get the number of times each element appears in the array.
Next, we hope to remove duplicate elements in the array after counting the frequency. The array_unique() function can help us complete the deduplication operation. The array after deduplication will only retain one instance of each element, but it should be noted that deduplication will not affect the frequency statistics of the element.
<?php
// Go to the heavy
$unique_array = array_unique($array);
// 输出Go to the heavy后的数组
print_r($unique_array);
?>
Output result:
Array
(
[0] => apple
[1] => banana
[3] => orange
)
After deduplication, only one duplicate element in the array is retained, although the frequency of the element has been counted through the array_count_values() function.
Based on the above two steps, we can write a complete example, first counting the frequency, then deduplication, and finally outputting the array and its element frequency after deduplication.
<?php
// Original array
$array = ["apple", "banana", "apple", "orange", "banana", "apple"];
// Statistical frequency
$frequencies = array_count_values($array);
// Go to the heavy
$unique_array = array_unique($array);
// Output frequency statistics results
echo "Frequency statistics:\n";
print_r($frequencies);
// 输出Go to the heavy后的数组
echo "Go to the heavy后的数组:\n";
print_r($unique_array);
?>
Output result:
Frequency statistics:
Array
(
[apple] => 3
[banana] => 2
[orange] => 1
)
Go to the heavy后的数组:
Array
(
[0] => apple
[1] => banana
[3] => orange
)
Through this example, we can not only count the frequency of occurrence of each element in the array, but also remove duplicate elements.
If you need to sort elements by frequency, you can use the arsort() function to sort the frequency array in descending order. Then, you can get the elements with higher frequency based on the sorted results.
<?php
// Sort frequency arrays in descending order
arsort($frequencies);
// Output the sorted frequency
echo "排序后的Frequency statistics:\n";
print_r($frequencies);
?>
Output result:
排序后的Frequency statistics:
Array
(
[apple] => 3
[banana] => 2
[orange] => 1
)
In this way, the frequency of elements appearing from high to low is sorted, which facilitates you to do further data processing.
The above is the complete method of combining the array_unique() function in PHP, first counting the frequency of elements in the array, and then deduplication. By using array_count_values() to count the frequency and combining array_unique() to deduplicate, you can flexibly process the data in the array to meet different needs.