d-_*_*_-b 1 php arrays frequency count
有没有办法在PHP中计算一个值在大数组中存在的频率?
所以,如果我有这样的数组:
$array = "1,2,3,4,joe,1,2,3,joe,joe,4,5,1,6,7,8,9,joe";
Run Code Online (Sandbox Code Playgroud)
有没有办法输出一个新的数组,告诉我(和排序)哪个最常用,多少个?
$result = array(
[joe] => 4
[1] => 3
[2] =>2
etc...
)
Run Code Online (Sandbox Code Playgroud)
我已经看过php array_count_values,但这可以按大多数排序 - >至少?或者有更简单的方法吗?
感谢大家!
用它们计算后对它们进行排序 arsort()
$result = array_count_values(explode(',', $array));
arsort($result);
Array
(
[joe] => 4
[1] => 3
[2] => 2
[4] => 2
[3] => 2
[9] => 1
[8] => 1
[5] => 1
[6] => 1
[7] => 1
)
Run Code Online (Sandbox Code Playgroud)