Link to home
Start Free TrialLog in
Avatar of DrDamnit
DrDamnitFlag for United States of America

asked on

Evaluate an array of boolean values to determine failures.

Given this array:
$results = [
		   ,true
		   ,true
		   ,true
		   ,true
		   ,true
		   ,true
		   ,true
		   ,true
		   ,false
		   ,true
		   ];

Open in new window


What is the most elegant way to evaluate this to so that the "sum" of these values is false? In other words, this is an array of test results. If there is a single failure, the entire set is a failure.

It seems I should be able to use xor or perhaps in_array(false)...
Avatar of John Smith
John Smith

Hi Michael,

This could be accomplished using array_search (http://php.net/manual/en/function.array-search.php). Simply search the array for 'false'. If it can't be found, array_search will return false, meaning that there are no failures. If it can be found, array_search will return the array key that contains 'false' which could be useful to you if you want to know what caused the failure.

Here's an example:

$evalResults = array_search('false', $results);

if ($evalResults == false) {
  echo "No failures";
} else {
    echo "Failure detected";
}

Open in new window


EDIT: I noticed that you mentioned in_array in the OP (http://php.net/manual/en/function.in-array.php). If you don't care to know what caused the failure, in_array is a good alternative.

Here's an example:

if (in_array("false", $results)) {
    echo "Failure detected";
} else {
    echo "No failures";
}

Open in new window


Either of the two options would get the job done.

I hope this helps
In case this is related to your other questions about PHPUnit, you can usually see the results on the screen when you run the unit tests.  A long all-green bar is a successful test.  If anything in there is red, the red unit test failed.  Better than just knowing a single failure occurred, the unit test protocol shows you exactly which test(s) failed!

Also, there is a risk in this code example (thanks a lot, PHP) because FALSE == 0, so if the first element with key 0 is the element with the failure...  also worth noting that 'false' is a string value, not a boolean.
$evalResults = array_search('false', $results);

if ($evalResults == false) { // THIS STATEMENT USES LOOSE TYPE COMPARISON
  echo "No failures";
} else {
    echo "Failure detected";
}

Open in new window

A better way would be to use strict-identity instead of loose-equality for the test
$evalResults = array_search(FALSE, $results);

if ($evalResults === FALSE) { // THIS STATEMENT USES STRICT COMPARISON
  echo "No failures";
} else {
    echo "Failure detected at location $evalResults";
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial