开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜