2 回答
TA贡献1886条经验 获得超2个赞
$old_array = array("1", "2" ,"3", "1", "5", "2", "2", "3", "3", "3", "6");
$values = array_count_values( $old_array );
arsort($values);
$result = array_keys($values);
输出(对于 php 5.6.35):
$result =
array(
(int) 0 => (int) 3,
(int) 1 => (int) 2,
(int) 2 => (int) 1,
(int) 3 => (int) 6,
(int) 4 => (int) 5
)
TA贡献1784条经验 获得超9个赞
一种选择是使用array_count_values,然后使用uksort根据原始数组中的值对键进行排序。
排序后,取array_keys。
$old_array = array("1", "2" ,"3", "1", "5", "2", "2", "3", "3", "3", "6");
$result = array_count_values($old_array);
uksort($result, function($a, $b) use ($result){
return $result[$a] < $result[$b];
});
$new_array = array_keys($result);
print_r($new_array);
输出
Array
(
[0] => 3
[1] => 2
[2] => 1
[3] => 5
[4] => 6
)
- 2 回答
- 0 关注
- 91 浏览
添加回答
举报