直接ctrl+V 慢慢试去吧~
<?php function mostRepeatedValues($array,$length=0){ //1. 计算数组的重复值 $array = array_count_values($array); //2. 根据重复值倒排序 arsort($array); if($length>0){ //3. 返回前 $length 重复值 $array = array_slice($array, 0, $length, true); } //取所有下标 $keys=array_keys($array); return $keys; } //$array = array(1, 1, 54, 3,4, 3,4, 3, 14, 3,4, 3,7,888,8,888,8,888,8,888,8,888,8,888,8,888,8,888,8,888,8,888,8,888,9,12,45,66,5,7,888,9,2,45); $t=microtime(true); $array=array(); for ($i2=1; $i2<=10; $i2++){ for ($i=1; $i<=20000; $i++){ array_push($array,$i); array_push($array,'888'); } } $t=microtime(true)-$t;$t=$t*1000; echo "运算".count($array)."个成员,耗时:".$t."<br>"; $counts=mostRepeatedValues($array,5);//要取重复次数最多的前多少个 print_r($counts); echo $counts[0];