%w(a b c a c d).uniq
得到各个元素出现的个数:
count_hash = {}
%w(a b c a c d).each do |item|
key = item.to_sym
if count = count_hash[key]
count_hash[key] = count + 1
else
count_hash[key] = 1
end
end
array_count_values(PHP 4, PHP 5, PHP 7)
array_count_values — 统计数组中所有的值
说明
array_count_values ( array $array ) : array
array_count_values() 返回一个数组: 数组的键是 array 里单元的值; 数组的值是 array 单元的值出现的次数。
参数
input
统计这个数组的值
返回值
返回一个关联数组,用 array 数组中的值作为键名,该值在数组中出现的次数作为值。
错误/异常
对数组里面的每个不是 string 和 integer 类型的元素抛出一个警告错误(E_WARNING)。
范例
示例 #1 array_count_values() 例子