简体   繁体   English

如何计算 PHP 数组中的非空条目?

[英]How to count non-empty entries in a PHP array?

Consider:考虑:

[name] => Array ( [1] => name#1
                  [2] => name#2
                  [3] => name#3
                  [4] => name#4
                  [5] =>
                  [6] =>
                  [7] =>
                  [8] =>
                  [9] =>
                )


$name = $_POST['name']

I want the result to be 4 .我希望结果是4

count ($name) = 9
count (isset($name)) = 1
count (!empty($name)) = 1

I would think that last one would accomplish what I need, but it is not (the empty entries are from unfilled inputs on the form).我认为最后一个可以完成我所需要的,但事实并非如此(空条目来自表单上未填写的输入)。

You can use array_filter to only keep the values that are “truthy” in the array, like this:您可以使用array_filter仅保留数组中“真实”的值,如下所示:

array_filter($array);

If you explicitly want only non- empty , or if your filter function is more complex:如果您明确地只想要非empty ,或者您的过滤器功能更复杂:

array_filter($array, function($x) { return !empty($x); });
# function(){} only works in in php >5.3, otherwise use create_function

So, to count only non-empty items, the same way as if you called empty(item) on each of them:因此,要仅计算非空项目,就像在每个项目上调用empty(item)一样:

count(array_filter($array, function($x) { return !empty($x); }));

However, as empty() is only a shorthand for 'variable is set and the value is truthy', you don't really gain anything from that, because if your array contains values such as FALSE or "0" , those would count as empty as well, same as with the plain call to array_filter .但是,由于empty()只是“设置了变量并且值是真实的”的简写,因此您并没有真正从中获得任何好处,因为如果您的数组包含诸如FALSE"0"类的值,则这些值将被视为也为空,与对array_filter的普通调用相同。

The better way is to be explicit about what you want to count, eg if you want to exclude empty strings only, then use:更好的方法是明确说明您要计算的内容,例如,如果您只想排除空字符串,请使用:

array_filter($array, function($x) { return ($x !== ""); });
count(array_filter($name));

Possible Solution: First you need to remove empty/null, false and zero values from an array and then count remaining values of an array可能的解决方案:首先,您需要从数组中删除空/null、false 和零值,然后计算数组的剩余值

If you no need to remove zero values from an array, but remove null and false values如果您不需要从数组中删除零值,但删除 null 和 false 值

count(array_filter($arrayName, 'strlen'));
//"strlen" use as second parameter if you no need to remove zero '0' values

if you need to remove zero, null and false values from an array如果您需要从数组中删除零、空和假值

count(array_filter($arrayName));

Here's a simple calculation function:这是一个简单的计算函数:

function non_empty(array $a) {
    return array_sum(array_map(function($b) {return empty($b) ? 0 : 1;}, $a));
}

This will preserve array indexes if your form handling function needs them, like when you're associating the third input on name to the third value of another input set, and there are empty inputs in between them.如果您的表单处理函数需要它们,这将保留数组索引,例如当您将 name 上的第三个输入关联到另一个输入集的第三个值时,并且它们之间有空输入。

The easyest aproach is to use count() and array_filter() functions (eventually with an additional callback within array_filter() when we need type compatibility checking, but when the result of default empty() function is enough for us we don't need it).最简单的方法是使用count()array_filter()函数(当我们需要类型兼容性检查时,最终在array_filter()中添加一个额外的回调,但是当默认的empty()函数的结果对我们来说已经足够时,我们不需要它)。

However, we can also achieve that by using the array_reduce() function , and this approach is slightly more optimal in terms of computational complexity:但是,我们也可以通过使用array_reduce()函数来实现,并且这种方法在计算复杂度方面稍微更优:

$initial = 0;
$count = array_reduce($arr, function($carry, $item) {
    $carry += empty($item) ? 0 : 1;
    return $carry;
}, $initial);

The $count variable counts all non-empty items in the array $arr . $count变量计算数组$arr中的所有非空项。

Note that, the condition can be changed if someone wishes, eg instead of empty($item) we can use a stronger condition $item === '' , etc.请注意,如果有人愿意,可以更改条件,例如,代替empty($item)我们可以使用更强的条件$item === ''等。

When the array $arr is empty, $initial is returned.当数组$arr为空时,返回$initial

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM