This article mainly introduces the method for calculating the top 10 most frequently occurring numbers in the PHP numeric array. It involves the usage skills of array_count_values, arsort, and other methods in php, which is of great practical value, friends in need
This article mainly introduces the method for calculating the top 10 most frequently occurring numbers in the PHP numeric array. It involves the usage skills of array_count_values, arsort, and other methods in php, which is of great practical value, friends in need
This example describes how to count the 10 most frequently used numbers in the PHP numeric array. Share it with you for your reference. The specific analysis is as follows:
This problem is in the TOPK category. It is often used to count the frequency of word occurrence, report, and data statistics!
The php code is as follows:
// Randomly generate a value array for ($ I = 0; $ I <1000; $ I ++) {$ ary [] = rand );} // count the number of occurrences of all values in the array $ ary = array_count_values ($ ary); arsort ($ ary); // sort values in reverse order $ I = 1; foreach ($ ary as $ key = >$ value) {if ($ I <= 10) {printf ("number: % d appears % d in total
", $ Key, $ value) ;}else {break ;}$ I ++;} unset ($ ary );
The result is as follows:
Number: 255 6 times in total number: 443 5 times in total number: 906 5 times in total number: 623 5 times in total number: 586 4 times in total number: 660 appear 4 times: 873 appear 4 times: 208 appear 4 times: 247 appear 4 times: 240 appear 4 times: appear 4 times: appear 4 times
I hope this article will help you with php programming.