Notice (8): unserialize(): Error at offset 16363 of 16373 bytes [APP/Controller/NewsController.php, line 5571]
Warning (2): Undefined array key "nsort" [APP/Controller/NewsController.php, line 3613]
Warning (2): Trying to access array offset on value of type null [APP/Controller/NewsController.php, line 3613]
Warning (2): Undefined array key "nsort" [APP/Controller/NewsController.php, line 3613]
Warning (2): Trying to access array offset on value of type null [APP/Controller/NewsController.php, line 3613]
Notice (8): unserialize() [<a href='https://secure.php.net/function.unserialize'>function.unserialize</a>]: Error at offset 12168 of 12277 bytes [APP/Controller/NewsController.php, line 5571]
PHP获取数组中重复最多的元素的方法 - 站长搜索
首页 > 资讯列表 > 编程/数据库 >>

PHP获取数组中重复最多的元素的方法

Warning (2): Undefined array key "nsort" [ROOT/plugins/Kuhuang/templates/Websites/view.php, line 430]
Warning (2): Trying to access array offset on value of type null [ROOT/plugins/Kuhuang/templates/Websites/view.php, line 430]
">
Warning (2): Undefined array key "nsort" [ROOT/plugins/Kuhuang/templates/Websites/view.php, line 430]
Warning (2): Trying to access array offset on value of type null [ROOT/plugins/Kuhuang/templates/Websites/view.php, line 430]
2014-12-02 22:22:40 转载来源: 网络整理/侵权必删

本文为大家讲解的是PHP获取数组中重复最多的元素的方法,感兴趣的同学参考下。   <?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;      }&

本文为大家讲解的是PHP获取数组重复多的元素方法,感兴趣的同学参考下。

 
<?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. 计算数组的重复值 
    $array = array_count_values($array); 
    //2. 根据重复值 倒排序 
    arsort($array); 
    if($length>0){ 
        //3. 返回前 $length 重复值 
        $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); 
/*输出结果为:
Array 

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

*/ 
?>

标签: PHP 获取 数组 重复 多的 元素 方法


声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!

站长搜索

http://www.adminso.com

Copyright @ 2007~2025 All Rights Reserved.

Powered By 站长搜索

打开手机扫描上面的二维码打开手机版


使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

站长搜索目录系统技术支持