PHP Statistical array of 10 occurrences of the most frequent number of methods
This article mainly introduces the PHP statistical array of 10 occurrences of the most frequent number of methods, involving PHP array_count_values and Arsort methods related to the use of skills, very practical value, the need for friends can refer to the following
The example in this article is about the 10 most frequently occurring numbers in a PHP statistical array. Share to everyone for your reference. The specific analysis is as follows:
The problem belongs to the category of TOPK, statistical words appear frequency, do report, data statistics will be used!
The PHP code is as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Randomly generated array of values for ($i =0; $i <1000; $i + +) { $ary []=rand (1,1000); } Number of occurrences of all values in the statistics array $ary =array_count_values ($ary); Arsort ($ary);//reverse order $i = 1; foreach ($ary as $key = = $value) { if ($i <=10) { printf ("Number:%d occurrences%d times" ", $key, $value); }else{ Break } $i + +; } Unset ($ary); |
The results are as follows:
?
1 2 3 4 5 6 7 8 9 10 |
Number: 255 Total occurrences 6 times Number: 443 total occurrences 5 times Number: 906 total occurrences 5 times Number: 623 Total occurrences 5 times Number: 586 Total occurrences 4 times Number: 660 Total occurrences 4 times Number: 873 Total occurrences 4 times Number: 208 Total occurrences 4 times Number: 247 Total occurrences 4 times Number: 240 Total occurrences 4 times |
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/987102.html www.bkjia.com true http://www.bkjia.com/PHPjc/987102.html techarticle PHP Statistical array of the most frequently occurring 10 numbers of the method this article mainly introduces the PHP statistical value array in the frequency of the most frequent occurrence of the 10 number of methods, involving PHP Array_cou ...