When performing data analysis, counting the frequency of a data set is a common and important operation, especially when processing user ratings or feedback. For example, on e-commerce platforms, counting the frequency of user ratings (such as 1-5 stars) can help companies understand the popularity of products and provide a basis for subsequent decisions.
PHP provides many powerful built-in functions, among which the array_count_values function is a tool that can quickly count the number of elements in an array. This article will introduce how to use PHP's array_count_values function to quickly count the frequency of user ratings to improve data analysis efficiency.
The array_count_values function is used to count the number of occurrences of each value in an array and returns an associative array where the key is the value in the array and the value is the number of occurrences of the corresponding value. Its basic syntax is as follows:
array_count_values(array $array): array
$array : an array that needs statistics.
The function returns a new array where the key is a different value in the input array, and the corresponding value is the number of times the value appears in the array.
Suppose we have a set of user rating data with a rating range of 1 to 5 stars. We will use array_count_values to count the frequency of each score.
<?php
// User rating data
$user_ratings = [5, 4, 4, 5, 3, 2, 5, 5, 4, 3, 2, 1, 5, 4, 3];
// use array_count_values Function statistics score frequency
$rating_counts = array_count_values($user_ratings);
// Output statistics
print_r($rating_counts);
?>
We define an array $user_ratings containing user ratings, which includes ratings between 1 and 5.
Call the array_count_values function to count the $user_ratings array and return a new associative array $rating_counts , its key is the rating value, and the value is the number of times the rating occurs.
Finally, use the print_r function to output the statistical results.
After executing the above code, we will get the following output:
Array
(
[5] => 5
[4] => 4
[3] => 3
[2] => 2
[1] => 1
)
This shows that:
Rating 5 appears 5 times
Rating 4 appears 4 times
Rating 3 appears 3 times
Rating 2 appears 2 times
Rating 1 appears once
The array_count_values function is especially suitable for processing scoring data, voting results, or any other data set that requires frequency statistics. For example, user comments on e-commerce platforms, number of likes on social media, ratings in the game, etc.
Using array_count_values not only makes the code more concise, but because the function is built-in to PHP, it is usually executed faster and can effectively improve the efficiency of data analysis. Especially when dealing with large amounts of data, built-in functions are usually more efficient than handwritten loop code.
The values in the input array of the array_count_values function are automatically converted to a string (if they are of integer type), which means that the returned array key will be a string even if the score value is an integer.
If the array contains duplicate elements, array_count_values correctly counts the number of occurrences of each element.
In some practical applications, we may want the statistical frequency data to be output in a certain order (for example from high to low). At this time, you can sort the statistical results in combination with arsort (sorted by descending value). For example:
<?php
// Sorting statistics results
arsort($rating_counts);
// Output sorted results
print_r($rating_counts);
?>
In this way, the output results will be arranged in the order of the rating frequency.