【发布时间】:2011-04-05 14:59:57
【问题描述】:
假设数组已排序,如何获取每个唯一项的计数
例子:
$array = array ("bye", "bye", "bye", "hello", "hello");
输出:
bye = 3
hello = 2
【问题讨论】:
标签: php
假设数组已排序,如何获取每个唯一项的计数
例子:
$array = array ("bye", "bye", "bye", "hello", "hello");
输出:
bye = 3
hello = 2
【问题讨论】:
标签: php
如果您想获取给定数组中指定列中唯一值的总数,可以将其作为一个简单的整数(而不是另一个数组)尝试如下简单的操作:
$uniqueCount = count(array_unique(array_column($data, 'column_name')));
// (where $data is your original array, and column_name is the column you want to cycle through to find the total unique values in whole array.)
var_dump(array_count_values(array("bye", "bye", "bye", "hello", "hello")));
【讨论】:
您可以使用array_count_values。
print_r(array_count_values($array));
将返回:
Array
(
[bye] => 3
[hello] => 2
)
【讨论】:
你可以在你的数组上使用array_count_values,它会返回如下内容:
array(2){
["bye"]=> int(3)
["hello"]=> int(2)
}
示例用法:
$unique = array_count_values($my_array);
【讨论】: