根据X值的计数和字母重新排序数组

时间:2016-03-14 21:42:11

标签: php arrays

例如我有一个类似的字符串。

d,b,a,a,b,a,c

有3'a',2'b',1'd',1'c'

然后我爆炸了:

[0] => d
[1] => b
[2] => a
[3] => a
[4] => b
[5] => a
[6] => c

array_unique(array)给了我这个:

[0] => d
[1] => b
[2] => a
[3] => c

我需要什么:

[0] => a
[1] => b
[2] => c
[3] => d

0 => a because there was 3 a in array.
1 => b because there was 2 b in array.
2 => c because there was 1 c in array. c is equal to d but it comes earlier in alphabet.
3 => d because there was 1 c in array. 

抱歉英语不好。

1 个答案:

答案 0 :(得分:1)

听起来你正在寻找这些方面的解决方案......

<?php
    $sampleString = "d,b,b,a,a,b,a,b,c";
    $sampleArray = explode(",",$sampleString);
    $arrayCounts = array_count_values($sampleArray);
    arsort($arrayCounts);
    $finalArray = array_keys($arrayCounts);
    print_r($finalArray);
?>