计值的数量在阵列的给定值 [英] Count number of values in array with a given value
本文介绍了计值的数量在阵列的给定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个这样的数组:
Say I have an array like this:
$array = array('', '', 'other', '', 'other');
我怎么能算与给定值的数字(在本例中为空)?
How can I count the number with a given value (in the example blank)?
和有效地做到这一点? (约十几阵列与数以百计每种元素的)
这个例子超时(超过30秒):
And do it efficiently? (for about a dozen arrays with hundreds of elements each) This example times out (over 30 sec):
function without($array) {
$counter = 0;
for($i = 0, $e = count($array); $i < $e; $i++) {
if(empty($array[$i])) {
$counter += 1;
}
}
return $counter;
}
在这种情况下,空白元件的数目是3
In this case the number of blank elements is 3.
推荐答案
如何使用 array_count _values 来获得一切的数组计算的吗?
How about using array_count _values to get an array with everything counted for you?
这篇关于计值的数量在阵列的给定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文