今天我看到一文章是写要我们如何获取数组中重复元素最多的那个,下面一起来看具体例子,代码如下:
<?php /** * * @author http://HdhCmsTestphpfensi测试数据 * Created on 2014-4-1 * @param array $array * @param int [optional] $length * @return array */ function mostRepeatedValues( $array , $length =0){ if ( empty empty ( $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://haodehen.cn/did31383