Running a loop- how do I check if all the items return TRUE?

I have a loop like below.

each item in the loop runs a function isavailable() that will return a TRUE or FALSE based on a check.


function checkdates($mydates){
  for ($i = 0; $i < $total ;$i++)
	{
   isavailable(mydates[$i]);
  }
}

Let’s say, I have to go through 5 items. ($total = 5).
IF all the ALL the items in the loop return TRUE, I want to do something. Otherswise, if any of them return true, I want to take another action. How do I check for this?

$total = count($mydates);
$trues = 0;

for ($i = 0; $i < $total; $i++) {
  if (isavailable($mydates[$i])) 
    $trues++;
}

if ($trues == $total) {
  //something
} else {
  //another action
}

function checkdates($mydates){
  
  $had_true = false;  //any true?
  $had_false = false; //any false?

  for ($i = 0; $i < $total ;$i++)
  {
    if(isavailable(mydates[$i])) {
      $had_true = true;
    }
    else {
      $had_false = true;
    }
  }
  
  //ALL were true
  if($had_true AND !$had_false) {
    #do something
  }
  
  //Some true, some false
  else if($had_true) {
     #do something
  }
}

Note: Lambda can be used in PHP 5.3


<?php
function isavailable($item) {
	return $item['stock'] > 0;
}

// test1
$items = array(array('stock'=>0),array('stock'=>1),array('stock'=>1));	
if(!in_array(false,array_map(create_function('$item','return isavailable($item);'),$items),true)) {
	echo '<p>1.) all item true</p>';
} else {
	echo '<p>1.) all items not true</p>';
}

// test 2
$items = array(array('stock'=>1),array('stock'=>1),array('stock'=>1));
if(!in_array(false,array_map(create_function('$item','return isavailable($item);'),$items),true)) {
	echo '<p>2.) all item true</p>';
} else {
	echo '<p>2.) all items not true</p>';
}
?>

There’s really a lot of ways. I’ll add some more.


    for ($i = 0; $i < $total; $i++) {
        if (!isavailable($mydates[$i])) {
            return false;
        }
    }
    return true;


    $allAvailable = true;
    for ($i = 0; $i < $total && $allAvailable; $i++) {
        $allAvailable = isavailable($mydates[$i]);
    }
    return $allAvailable;


    foreach ($mydates as $date) {
        if (!isAvailable($date)) {
            return false;
        }
    }
    return true;

php 5.3+


$all = function($array, $callback) {
    foreach ($array as $val) {
        if (!call_user_func($callback, $val)) {
            return false;
        }
    }
    return true;
};

if ($all($mydates, 'isAvailable')) {
    
}

You wouldn’t normally do the last one in php though.

crmalibu - I don’t think your first example (and possibly others) meets the OP requirements. It will return false as soon as one date fails. The first could be false, and the next true, but you’d never know because the whole array wasn’t checked. Bruin03 needs to know if all were true, or if ANY were true.

Same with Dan’s example, it doesn’t differentiate between all false, and some true.

all… yep that ruby rubbing off,lol

Ah, I guess I overlooked that last part

if ($trues == $total) {
  //something
} else if ($trues > 0) {
  //other thing
}

I got that from javascript. I haven’t drunk the ruby koolaid (yet).

Doh. I completely missed that part.

One more for good luck :S
returns 0, -1, 1 for some true, none or all true.


function allSomeNone($arr) {
	$t = $f = 0;
	foreach($arr as $v) {
		if(isAvail($v)) $t = 1;
		else $f = -1;
	}
	return $t+$f;
}

Spot on!

Thank you Cranial bore!
Yours and Dans examples make sense. I appreciate everyone who attempted to help and got some good ideas. :slight_smile:

I really like oddz’s solution. You can essentially put everything into your one function. Check this out:


/*
 * Returns true if item or all items are avaiable
 * @param mixed (int or array of int)
 * @return bool
 */
    function isAvailable($item=null) {
    
        // Check through the whole array
        if (is_array($item)) {
            return !in_array(false, array_map("isAvailable", $item));
        }
    
        // Check for one item
        return $item > 0;
    }

/** Tests **/

// Array, True
if (isAvailable($items = array(23, 4, 6))) {
    echo 'Array is TRUE<br>';
}
else {
    echo 'Array is FALSE<br>';
}

// Array, False
if (isAvailable($items = array(0, 4, 6))) {
    echo 'Array is TRUE<br>';
}
else {
    echo 'Array is FALSE<br>';
}

// Single Val, True
if (isAvailable(7)) {
    echo 'Int is TRUE<br>';
}
else {
    echo 'Int is FALSE<br>';
}

// Single Val, False
if (isAvailable(0)) {
    echo 'Int is TRUE<br>';
}
else {
    echo 'Int is FALSE<br>';
}

Ouputs:
Array is TRUE
Array is FALSE
Int is TRUE
Int is FALSE

And if all you were doing was checking a string of numbers, and you wanted it to be a number Not 0, you could simplify it to this (if you knew there would never be any negatives… so the 1st version i posted is better):


    function isAvailable($item=null) {
    
        // Check through the whole array
        if (is_array($item)) {
            return !in_array(false, $item);
        }
    
        // Check for one item
        return $item;
    }

Thanks tmapm :slight_smile:

Interesting approach