欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP獲取數(shù)組中重復(fù)最多的元素的實(shí)現(xiàn)方法

 更新時(shí)間:2014年11月11日 12:04:12   投稿:shichen2014  
這篇文章主要介紹了PHP獲取數(shù)組中重復(fù)最多的元素的實(shí)現(xiàn)方法,通過一個(gè)自定義函數(shù)遍歷數(shù)組實(shí)現(xiàn)這一功能,是非常使用的技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP獲取數(shù)組中重復(fù)最多的元素的實(shí)現(xiàn)方法。分享給大家供大家參考。具體方法如下:

復(fù)制代碼 代碼如下:
<?php 
/** 
 *  
 * Created on 2014-4-1 
 * @param   array $array 
 * @param   int [optional] $length 
 * @return  array 
 */ 
function mostRepeatedValues($array,$length=0){ 
    if(emptyempty($array) or !is_array($array)){ 
        return false; 
    } 
    //1. 計(jì)算數(shù)組的重復(fù)值 
    $array = array_count_values($array); 
    //2. 根據(jù)重復(fù)值 倒排序 
    arsort($array); 
    if($length>0){ 
        //3. 返回前 $length 重復(fù)值 
        $array = array_slice($array, 0, $length, true); 
    } 
    return $array; 

$array = array(1, 1, 1, 54, 3,4, 3,4, 3, 14, 3,4, 3,7,8,9,12,45,66,5,7,8,9,2,45); 
$counts=mostRepeatedValues($array,5); 
print_r($counts); 
/*輸出結(jié)果為:
Array 

    [3] => 5 
    [4] => 3 
    [1] => 3 
    [9] => 2 
    [45] => 2 

*/ 
?>

希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論