A method of 10 digits with the most frequency in the array of PHP statistical values

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.