php取最热5个号码,字符串和数组转换各种数组操作的实战例子

php取最热5个号码,字符串和数组转换各种数组操作的实战例子

//按值由高到低排序
        arsort($ct);
        $data['ct'] = $ct;
        
        //取最热的5个号码
        $hotnums = '';
        foreach($ct as $key=>$val){
            //号码作为字符串累加
            $hotnums = $hotnums.$key;
            //字符串转成数组
            $arr = str_split($hotnums);
            //数组去重
            $arr = array_unique($arr);
            //统计数组个数
            $ncount = count($arr);
            //大于等于5个时处理
            if($ncount >= 5){
                //取排序靠前的5个
                $arr = array_slice($arr,0,5);
                //从小到大排序
                sort($arr);
                //数组转成字符串
                $hotnums = implode("", $arr);
                break;
            }
        }
        $data['hotnums'] = $hotnums;

原文地址:https://www.cnblogs.com/zdz8207/p/php-array-string-sort.html