Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortest way to evaluate an array with booleans (PHP)?

I have an array with booleans in it, whats the shortest way to loop through it and get the final boolean expression?

Example: array with 3 elems (true, false, true) --> the result should be false (true && false && true = false)

I know I can loop through it, but is there a shorter way?

Thanks!

like image 879
EOB Avatar asked Apr 25 '12 11:04

EOB


People also ask

How do I specify a boolean value in PHP?

Syntax To specify a boolliteral, use the constants trueor false. Both are case-insensitive. <?php $foo = True; // assign the value TRUE to $foo

How do you evaluate an empty array in PHP?

In PHP, an empty array evaluates to false, while in JavaScript an empty array evaluates to true. In PHP, you can test an empty array as <?php if (!$stuff) …; ?> which won’t work in JavaScript where you need to test the array length.

How to display the value of a Boolean as 1 or 0?

But there may be times when it might be helpful to see the value of the Boolean as a 1 or 0. Here's how to do it. <?php $var1 = TRUE; $var2 = FALSE; echo $var1; // Will display the number 1 echo $var2; //Will display nothing /* To get it to display the number 0 for

How to cast $a to bool in PHP?

$a = !array('a'); // This will === false; $s = ! ""; // This will === true; $s = !"hello"; // This will === false; To cast as if using a (bool) you can NOT the NOT with "!!" (double '!'), then you are casting to the correct (bool). Example: <?php $a = !!array(); // This will === false; (as expected)


2 Answers

if (!in_array(false, $array)) { ...
like image 78
Björn Avatar answered Sep 30 '22 19:09

Björn


I don't knw php but you can

search for false in array first

if search is found then result is false else result is true.

Not: It's just and algorithm. not a code

like image 32
Red Avatar answered Sep 30 '22 20:09

Red