$array = array(
'vegs' => 'tomatos',
'cheese' => false,
'nuts' => 765,
'' => null,
'fruits' => 'apples'
);
var_dump(in_array(false, $array, true)); //returns true because there is a false value
How to check strictly if there is at least one NON-false (string, true, int) value in array using in_array only or anything but not foreach?
var_dump(in_array(!false, $array, true)); //this checks only for boolean true values
var_dump(!in_array(false, $array, true)); //this returns true when all values are non-false
Actual solution below
Just put the negation at the right position:
Of course this will also be true if the array contains no elements at all, so what you want is:
Edit: forget it, that was the answer for “array contains only values that are not exactly false”, not “array contains at least one value that is not exactly false”
Actual solution:
array_filterreturns an array with all values that are!== false, if it is not empty, your condition is true.Or simplified as suggested in the comments:
Of course, the cast to
boolalso can be omitted if used in anifclause.