0

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';
2

5 Answers 5

5
$missing_permission = array_search(false, $l);
if($missing_permission !== false) {
    echo 'No way!';
} else {
    echo 'OK!';
}
Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

1

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.

Comments

0

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.

Comments

0

Loop through the array. If you encounter a false element, stop and print "Can not do this". If you encounter the end of the array, stop and print "Can do this".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.