Php 如何根据术语的频率对数组进行排序?
假设我有这样一个数组:Php 如何根据术语的频率对数组进行排序?,php,arrays,sorting,repeat,Php,Arrays,Sorting,Repeat,假设我有这样一个数组: $array = array('a', 'b', 'c', 'c', 'c', 'd', 'a', 'b', 'b', 'b', 'b'); ['b', 'c', 'a', 'd'] 我要做的是返回一个数组,该数组按包含项的频率重新排序。那么像这样, $array = array('a', 'b', 'c', 'c', 'c', 'd', 'a', 'b', 'b', 'b', 'b'); ['b', 'c', 'a', 'd'] 因为b出现了5次,c出现了三次,
$array = array('a', 'b', 'c', 'c', 'c', 'd', 'a', 'b', 'b', 'b', 'b');
['b', 'c', 'a', 'd']
我要做的是返回一个数组,该数组按包含项的频率重新排序。那么像这样,
$array = array('a', 'b', 'c', 'c', 'c', 'd', 'a', 'b', 'b', 'b', 'b');
['b', 'c', 'a', 'd']
因为b
出现了5次,c
出现了三次,a
出现了两次,d
只出现了一次。如何做到这一点 这应该有帮助
$counts = array_count_values($array);
arsort($counts);
$list = array_keys($counts);
var_dump($list);
直接从手册页
描述
数组\u计数\u值(数组
$input)数组\计数\值()返回
使用输入值的数组
数组作为键及其在中的频率
作为值输入
然后根据这些值重新排序,这应该会有所帮助
直接从手册页
描述
数组\u计数\u值(数组
$input)数组\计数\值()返回
使用输入值的数组
数组作为键及其在中的频率
作为值输入
然后根据这些值重新排序