Can boolean needle be passed to in_array?
Can I pass false
as开发者_StackOverflow中文版 a needle to in_array()
?
if(in_array(false,$haystack_array)){
return '!';
}else{
return 'C';
}
The $haystack_array
will contain only boolean values. The haystack represents the results of multiple write queries. I'm trying to find out if any of them returned false, and therefore not completed.
PHP won't care what you pass in as your 'needle', but you should probably also use the third (optional) parameter for in_array to make it a 'strict' comparison. 'false' in PHP will test as equal to 0
, ''
, ""
, null
, etc...
Yep, just like in your example code. Because in_array
returns a boolean to indicate whether the search was successful (rather than returning the match), it won't cause any problems.
There's got to be a better way.
<?php
echo (in_array(false,array(true,true,true,false)) ? '!' : 'C');
echo (in_array(false,array(true,true,true,true)) ? '!' : 'C');
Output:
!C
Yes you can but why don't you do it with a single boolean variable like this:
$anyFalseResults = false;
... your query loop {
// do the query
if( $queryResult == false ) $anyFalseResults = true;
}
at the end of the loop $anyFalseResults will contain what you are looking for.
精彩评论