php 使用给定值计算数组中值的数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1317612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:00:48  来源:igfitidea点击:

Count number of values in array with a given value

phparrayscount

提问by Tom

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)?

如何计算具有给定值的数字(在示例空白中)?

And do it efficiently? (for about a dozen arrays with hundreds of elements each) This example times out (over 30 sec):

并有效地做到这一点?(大约十几个数组,每个数组有数百个元素)这个例子超时(超过 30 秒):

function without($array) {
    $counter = 0;
    for($i = 0, $e = count($array); $i < $e; $i++) {
        if(empty($array[$i])) {
            $counter += 1;
        }
    }
    return $counter;
}

In this case the number of blank elements is 3.

在这种情况下,空白元素的数量是 3。

回答by Cellfish

How about using array_count _valuesto get an array with everything counted for you?

如何使用array_count _values来获取一个数组,其中的所有内容都为您计数?

回答by Sampson

Just an idea, you could use array_keys( $myArray, "" )using the optional second parameter which specifies a search-value. Then count the result.

只是一个想法,您可以使用array_keys( $myArray, "" )指定搜索值的可选第二个参数。然后计算结果。

$myArray = array( "","","other","","other" );
$length  = count( array_keys( $myArray, "" ));

回答by camomileCase

I dont know if this would be faster but it's something to try:

我不知道这是否会更快,但可以尝试:

$counter = 0;
foreach($array as $value)
{
  if($value === '')
    $counter++;
}
echo $counter;

回答by Steve

You could also try array_reduce, with a function which would just count the value you are interested in. eg

您也可以尝试使用array_reduce函数,它只会计算您感兴趣的值。例如

function is_empty( $v, $w )
{ return empty( $w ) ? ($v + 1) : $v; }

array_reduce( $array, 'is_empty', 0 );

Some benchmarking might tell you if this is faster than array_count_values()

一些基准测试可能会告诉您这是否比 array_count_values() 快

回答by Darren

Generally for counting blanks only. Really depends on use case and speed needed. Personally I like doing things one one line.

通常仅用于计算空白。真的取决于用例和所需的速度。我个人喜欢一行一行地做事。

Like the chosen response though But you still need a line to extract the data needed though to another variable.

就像选择的响应但是您仍然需要一行来将所需的数据提取到另一个变量中。

$r = count($x) - count(array_filter($x));

回答by Bhushan Rana

We use array_filter function to find out number of values in array

我们使用 array_filter 函数来找出数组中值的数量

$array=array('','','other','','other');
$filled_array=array_filter($array);// will return only filled values
 $count=count($filled_array);
echo $count;// returns array count

回答by Swaran

function countarray($array)
{        $count=count($array);         
         return $count;        
}        
$test=$array = array('', '', 'other', '', 'other');        
echo countarray($test);

回答by user2964153

function arrayvaluecount($array) {

    $counter = 0;
    foreach($array as $val){

        list($v)=$val;
        if($v){

        $counter =$counter+1;
        }

    }
return $counter;
}