说我有这样的数组:
$array = array('', '', 'other', '', 'other');
Run Code Online (Sandbox Code Playgroud)
如何计算给定值的数字(在示例空白中)?
并且有效地做到了吗?(对于大约12个阵列,每个阵列有数百个元素)这个例子超时(超过30秒):
function without($array) {
$counter = 0;
for($i = 0, $e = count($array); $i < $e; $i++) {
if(empty($array[$i])) {
$counter += 1;
}
}
return $counter;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,空白元素的数量是3.
如何计算数组中此值的实例数?
例如:
$array = array(5,5,5,5,3,3,2,1,2,4,5,6,7);
Run Code Online (Sandbox Code Playgroud)
如果我想计算在那里有多少5个我怎么能这样做?
我以为我应该使用count但count会计算数组中的所有值而不是特定值?我搜索了stackoverflow,发现没有这样的问题