是否有一个PHP函数来计算一个阵列内发生的值的次数? [英] Is there a PHP function to count the number of times a value occurs within an array?

查看:101
本文介绍了是否有一个PHP函数来计算一个阵列内发生的值的次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要算定数组中出现值的次数。

I need to count the number of times a value occurs in a given array.

例如:

$array = array(5, 5, 2, 1);

// 5 = 2 times
// 2 = 1 time
// 1 = 1 time

有没有这样的功能存在吗?如果是这样,请点我给它的PHP文件...因为我似乎无法找到它。

Does such a function exist? If so, please point me to it in the php docs... because I can't seem to find it.

感谢。

推荐答案

是的,这就是所谓的 array_count_values​​()

Yes, it's called array_count_values().

$array = array(5, 5, 2, 1);
$counts = array_count_values($array); // Array(5 => 2, 2 => 1, 1 => 1)

这篇关于是否有一个PHP函数来计算一个阵列内发生的值的次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆