根据重复值排序PHP数组 [英] Ordering PHP arrays based on duplicate values

查看:463
本文介绍了根据重复值排序PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含重复值的数组.我想对数组进行排序,以使重复次数最多的值排在第一位.这是我的数组的示例:

I have an array which contains duplicate values. I want to sort the array so that the values with the most duplicates appear first in line. Here's an example of my array:

array(1, 2, 3, 2, 1, 2, 2);

我想对该数组进行排序,以便根据重复量将其自身排序为以下内容:

I want to sort this array so that it orders itself based on the amount of duplicates into the following:

array(2, 1, 3);

'2'具有最多的重复项,因此其排在第一位,其次是值,将减少重复项.有人知道我该怎么做吗?

'2' has the most duplicates so it's sorted first, followed by values will less duplicates. Does anyone know how I can accomplish this?

推荐答案

 $acv=array_count_values($array); //  1=>2, 2=>3,3=>1
 arsort($acv); //save keys,           2=>3, 1=>2, 3=>1 
 $result=array_keys($acv); //get only keys   2,1,3

这篇关于根据重复值排序PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆