Home  >  Article  >  Backend Development  >  PHP method to count the 10 most frequently occurring numbers in a numerical array_PHP Tutorial

PHP method to count the 10 most frequently occurring numbers in a numerical array_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 09:56:41790browse

How to count the 10 most frequently occurring numbers in a numerical array in PHP

This article mainly introduces the method of counting the 10 most frequently occurring numbers in a numerical array in PHP. Related usage skills involving array_count_values ​​and arsort methods in php are very practical. Friends in need can refer to it

The example in this article describes the method of PHP counting the 10 most frequently occurring numbers in a numerical array. Share it with everyone for your reference. The specific analysis is as follows:

This question belongs to the TOPK category. It is used to count the frequency of word occurrences and make reports. It is commonly used in data statistics!

The php code is as follows:

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

//随机生成数值数组

for($i=0;$i<1000;$i ){

$ary[]=rand(1,1000);

}

//统计数组中所有的值出现的次数

$ary=array_count_values($ary);

arsort($ary);//倒序排序

$i=1;

foreach($ary as $key=>$value){

if($i<=10){

printf("数字:%d 共出现 %d 次
",$key,$value);

}else{

break;

}

$i ;

}

unset($ary);

1

2

3

1

2

3

4

5

6

7

8

9

10

数字:255 共出现 6 次

数字:443 共出现 5 次

数字:906 共出现 5 次

数字:623 共出现 5 次

数字:586 共出现 4 次

数字:660 共出现 4 次

数字:873 共出现 4 次

数字:208 共出现 4 次

数字:247 共出现 4 次

数字:240 共出现 4 次

4

5

6

8 9 10 11 12
13 14
15 16 17
//Randomly generate numerical array for($i=0;$i<1000;$i ){<🎜> <🎜>$ary[]=rand(1,1000);<🎜> <🎜>}<🎜> <🎜>//Count the number of occurrences of all values ​​in the array<🎜> <🎜>$ary=array_count_values($ary);<🎜> <🎜>arsort($ary);//Sort in reverse order<🎜> <🎜>$i=1;<🎜> <🎜>foreach($ary as $key=>$value){ if($i<=10){<🎜> <🎜>printf("Number: %d appears %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 appears 6 times in total Number: 443 Appeared 5 times in total Number: 906 appears 5 times in total Number: 623 appears 5 times in total Number: 586 appears 4 times in total Number: 660 appears 4 times in total Number: 873 appears 4 times in total Number: 208 appears 4 times in total Number: 247 appears 4 times in total Number: 240 appears 4 times in total
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/987102.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/987102.htmlTechArticleHow to count the 10 most frequently occurring numbers in a numerical array using PHP. This article mainly introduces PHP counting numerical arrays. The method of the 10 most frequently occurring numbers in php involves array_cou...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn