2 回答

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊
$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);
輸出(對(duì)于 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貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊
一種選擇是使用array_count_values,然后使用uksort根據(jù)原始數(shù)組中的值對(duì)鍵進(jìn)行排序。
排序后,取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 關(guān)注
- 123 瀏覽
添加回答
舉報(bào)