This article mainly introduces the PHP statistical value array in the frequency of the most frequent 10 numbers of methods, involving PHP array_count_values and Arsort and other methods related to the use of skills, very practical value, the need for friends can refer to the
This example describes a method that has the most frequent 10 digits in an array of PHP statistical values. Share to everyone for your reference. The specific analysis is as follows:
This problem belongs to the category of TOPK, statistics words appear frequency, do the 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 generate numeric array for ($i =0 $i <1000; $i + +) {$ary []=rand (1,1000);}//Statistics the number of values in the array appears $ary =array_count_values ($ary); Arsort ($ary);//reverse order $i = 1; foreach ($ary as $key => $value) {if ($i <=10) {printf ("Number:%d%d times <br/>", $key, $value);} else{break;} $i + +; } unset ($ary); |
The results are as follows:
?
| 1 2 3 4 5 6 7 8 9 10 |
Number: 255 There are 6 times the number: 443 total appears 5 times: 906 There are 5 times: 623 There are 5 times: 586 There are 4 times: 660 of the total appears 4 times the number: 873 there are 4 times: 208 There are 4 figures : 247 A total of 4 times: 240 Total appeared 4 times |
I hope this article will help you with your PHP program design.