PHP 统计数值数组中出现频率最多的10个数字(TOP K问题)
Jun 06, 2016 pm 07:32 PM
phptAppearnumerical valuenumberarraystatisticsfrequency
TOPK范畴,统计单词出现频率,做报表,数据统计的时会常用! 无 //随机生成数值数组 for($i=0;$i1000;$i++){ $ary[]=rand(1,1000);}$ary=array_count_values($ary);//统计数组中所有的值出现的次数arsort($ary);//倒序排序$i=1;foreach($ary as $key=$value){
TOPK范畴,统计单词出现频率,做报表,数据统计的时会常用!
//随机生成数值数组 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); //结果 数字:255 共出现 6 次 数字:443 共出现 5 次 数字:906 共出现 5 次 数字:623 共出现 5 次 数字:586 共出现 4 次 数字:660 共出现 4 次 数字:873 共出现 4 次 数字:208 共出现 4 次 数字:247 共出现 4 次 数字:240 共出现 4 次
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
2 weeks agoByDDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor