looking for something like "array_and"
I'm want to check an array like this, if every value is "true"
$arr = array(true开发者_如何学Go, true, true) // would be true
$arr = array(true, true, false) // would be false
$arr = array(false, true, false) // would be false
PHP provides a funcion "array_sum()". Is there a short function like "array_and()"
Currently I use something like that:
$result = true;
foreach ($arr as $item) {
$result = $result && $item;
}
Is there any shorter solution?
It looks like your are ANDing the array contents. If that is the case you need only ask if false occurs anywhere in the array. So:
$result=in_array(false, $arr,true);
array_product would work just fine for you.
What? A dirty hack? Well, this is the php way of doing things.
If I understand the question you could use in_array("false")
.
$foo = array(true, true, false);
if(in_array(false, $foo)) {
echo 'false is present';
}
could be one option unless i'm missing something
精彩评论