I have an array containing only booleans:
$l = array(0=>true,2=>false,3=>true,4=>true,5=>true);
I want to achieve this pseudocode:
If at least one element of $l is false:
echo 'Can not do this';
else:
echo 'Can do this';
I have an array containing only booleans:
$l = array(0=>true,2=>false,3=>true,4=>true,5=>true);
I want to achieve this pseudocode:
If at least one element of $l is false:
echo 'Can not do this';
else:
echo 'Can do this';
You can simply use the in_array function of php, which checks the array for a given value.
$l = array(0=> true, 2 => false, 3 => true, 4 => true, 5 => true);
if (in_array(FALSE, $l)) {
echo "can not do this";
} else {
echo "can do this";
}
Example: http://www.ideone.com/yv3Yf
You might try to use array_reduce function. Something like this:false elements.
array_reduce($data, function($v, $w){ if ($w == false) { $v++; } return $v; return 1; } , 0);
Call back function counts number of false elements in array. But I variant with array_search or in_array meantion before is better. Try not to use cycles of php using standard php functions which is much faster as it uses native c code.
Assuming you have a misspelling and the correct form is
if at least one element in array $l is false
Then you only need to iterate over the array and fail on the first false value in the array. I will not give you the answer, since it is homework and the solution is very simple.
Take a look at the if statement in the PHP manual and also the foreach statement. That's really all you need to solve your problem.