Reputation: 995
I have run into a point in my code where I would like to check if, within a dynamically set array, there are at least two set values before performing certain tasks. I cannot find a native PHP function which accomplishes this. Count returns all values, but does not allow me to check for the "truthiness" of the array values. Is there an equivalent native function for the following code:
function count_set($array = array()){
$count = 0;
foreach($array as $key => $value){
if($value){
$count++;
}
}
return $count;
}
Upvotes: 1
Views: 58
Reputation: 78994
Truthy scalar values are NOT false
, 0
, null
, string 0
or an empty string (see Converting to boolean). array_filter()
will remove these by default if you don't provide a callback:
$count = count(array_filter($array));
Upvotes: 2