Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Count Number of True Values in a Boolean Array

I have an associative array in which I need to count the number of boolean true values within.

The end result is to create an if statement in which would return true when only one true value exists within the array. It would need to return false if there are more then one true values within the array, or if there are no true values within the array.

I know the best route would be to use count and in_array in some form. I'm not sure this would work, just off the top of my head but even if it does, is this the best way?

$array(a->true,b->false,c->true)    

if (count(in_array(true,$array,true)) == 1)
{
    return true
}
else
{
    return false
}
like image 572
ThatTechGuy Avatar asked May 07 '13 21:05

ThatTechGuy


People also ask

How do I count the number of values in an array in PHP?

How to Count all Elements or Values in an Array in PHP. We can use the PHP count() or sizeof() function to get the particular number of elements or values in an array. The count() and sizeof() function returns 0 for a variable that we can initialize with an empty array.

How can check boolean value in if condition in PHP?

The is_bool() function checks whether a variable is a boolean or not. This function returns true (1) if the variable is a boolean, otherwise it returns false/nothing.

How can I get multidimensional array count in PHP?

Syntax – count() If you pass multidimensional array, count($arr) returns length of array only in the first dimension. If you want to count all elements in a multidimensional array recursively, pass COUNT_RECURSIVE as second argument, as shown in the following code sample.

What is the use of Array_count_values () in PHP explain with example?

The array_count_values() is an inbuilt function in PHP which is used to count all the values inside an array. In other words we can say that array_count_values() function is used to calculate the frequency of all of the elements of an array.


3 Answers

I would use array_filter.

$array = array(true, true, false, false);
echo count(array_filter($array));
//outputs: 2

Array_filter will remove values that are false-y (value == false). Then just get a count. If you need to filter based on some special value, like if you are looking for a specific value, array_filter accepts an optional second parameter that is a function you can define to return whether a value is true (not filtered) or false (filtered out).

like image 170
Jonathan Kuhn Avatar answered Oct 06 '22 13:10

Jonathan Kuhn


Since TRUE is casted to 1 and FALSE is casted to 0. You can also use array_sum

$array = array('a'=>true,'b'=>false,'c'=>true);
if(array_sum($array) == 1) {
    //one and only one true in the array
}

From the doc : "FALSE will yield 0 (zero), and TRUE will yield 1 (one)."

like image 39
Frédéric Clausset Avatar answered Oct 06 '22 12:10

Frédéric Clausset


Try this approach :

<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>

Result :

Array
(
   [1] => 2
   [hello] => 2
   [world] => 1
)

Documentation

like image 27
ROMMEL Avatar answered Oct 06 '22 12:10

ROMMEL