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)...
LVL 32
DrDamnitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_TyrantCommented:
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
Ray PaseurCommented:
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

Ray PaseurCommented:
The accepted solution is wrong.  For better or worse, some members of E-E do not test their code before they post it here, so we have to deal with that caveat.

PHP is a loosely typed language.  Things are either "falsy" or "truthy" depending on the context in which they are used.  In particular, a non-zero, non-null string variable is "truthy."  To the instant case, the string "false" evaluates to the boolean TRUE under loose comparison rules.  The equality test is the wrong test for boolean values.  The identicality test is appropriate here.  Man page references are included in the code snippet below.

Please see:  http://iconoun.com/demo/temp_michael_munger.php
<?php // demo/temp_michael_munger.php
/**
 * http://www.experts-exchange.com/questions/28911169/Evaluate-an-array-of-boolean-values-to-determine-failures.html#a41401744
 *
 * http://php.net/manual/en/language.types.php
 * http://php.net/manual/en/language.operators.comparison.php
 * http://php.net/manual/en/language.types.type-juggling.php
 *
 * http://php.net/manual/en/types.comparisons.php
 */
error_reporting(E_ALL);

// TEST DATA FROM THE POST AT E-E
$results = [
		   true // NOTE IT WAS NECESSARY TO REMOVE A PARSE ERROR - EXTRANEOUS COMMA HERE
		   ,true
		   ,true
		   ,true
		   ,true
		   ,true
		   ,true
		   ,true
		   ,false
		   ,true
		   ];

// SHOW THE TEST DATA
echo '<pre>';
var_dump($results);
echo PHP_EOL;


// ONE OF THE ACCEPTED SOLUTIONS
$evalResults = array_search('false', $results);

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

echo PHP_EOL;


// ANOTHER OF THE ACCEPTED SOLUTIONS
if (in_array("false", $results)) {
    echo "Failure detected";
} else {
    echo "No failures";
}

echo PHP_EOL;


// A CORRECT SOLUTION
$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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
_TyrantCommented:
Hi Michael,

Ray is correct - I made a mistake in the example that I provided. I should have used strict comparison ($evalResults === FALSE) as he did in the modified version.

Not to further confuse the issue, but I noticed that my example and Ray's modified example don't take into account the possibility of multiple failures. Sure, any failure (one or more) would result in a message indicating that there is a failure, but the line below assumes only one failure because array_search returns the key of the first occurrence of the search term.

echo "Failure detected at location $evalResults";

Open in new window


If it's important for you to know the key of every occurrence, then the following code might better suited.

$failures = (array_keys($results, false, true));

if ($failures) {
	
	foreach ($failures as &$location){
		$output = $output . $location . ", ";
	}
	
	echo "Failure detected at location: ";
	echo substr($output, 0, (strlen($output) - 2));

} else {
	echo "No failures detected";
}

Open in new window


You can read more about array_keys and see more examples here: http://php.net/manual/en/function.array-keys.php.

I hope this helps.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.