Home > php教程 > php手册 > body text

PHP统计数值数组中出现频率最多的10个数字的方法,数值数组

WBOY
Release: 2016-06-13 09:06:27
Original
938 people have browsed it

PHP统计数值数组中出现频率最多的10个数字的方法,数值数组

本文实例讲述了PHP统计数值数组中出现频率最多的10个数字的方法。分享给大家供大家参考。具体分析如下:

该问题属于TOPK范畴,统计单词出现频率,做报表,数据统计的时会常用!

php代码如下:

//随机生成数值数组
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 次<br/>",$key,$value); 
  }else{
    break;
  }
  $i++;
}
unset($ary);
Copy after login

结果如下:

数字:255 共出现 6 次
数字:443 共出现 5 次
数字:906 共出现 5 次
数字:623 共出现 5 次
数字:586 共出现 4 次
数字:660 共出现 4 次
数字:873 共出现 4 次
数字:208 共出现 4 次
数字:247 共出现 4 次
数字:240 共出现 4 次
Copy after login

希望本文所述对大家的php程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!