PHP Statistical numerical array of the most frequently occurring 10 numbers method _php Tutorial

Source: Internet
Author: User

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 ...

  • 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.