我有一个这样的数组:
"New York",
"Chicago",
"Los Angeles",
"Boston",
"New York",
"Miami",
"Houston",
"New York",
"Miami",
"Chicago",
...
我现在想要的是a)按键对数组进行分组,b)计算数量,c)按数量排序。
之后,新的阵列应该是这样的:
"New York" => 2,
"Miami" => 2,
"Chicago" => 2,
"Los Angeles" => 1,
"Boston" => 1,
"Houston" => 1
有什么想法吗?
非常简单。
只需使用以下代码:
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
将输出以下
Array
(
[1] => 2
[hello] => 2
[world] => 1
)
参见array_count_values