I have the following code:
$data = array(); // prep array
$data['aardvark'] = true;
print_r($data); // output array
echo "\n";
var_dump(in_array('zebra', $data));
The output is as follows:
Array
(
[aardvark] => 1
)
bool(true)
Despite the fact that zebra
is clearly not in the array. It looks like it's to do with PHP's loose type system. (bool) 'zebra'
is true
, and there's a true
in the array so the in_array
returns true?
I think I can see the logic, but it's flawed. Is this a PHP bug?
Cheers.
Not a bug. You have it exactly right. To correctly find what you are looking for, you will have to do this:
if (in_array('zebra', $data, true)) {
Although this would probably be rare that you store different data types in the same array (strings and booleans). If you are storing data that are not a list, you should most likely be using an object instead.
You are right. in_array()
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
You must set the third parameter to true
, if you want in_array()
to test against the type too. Otherwise it will make loose comparison
var_dump(in_array('zebra', $data, true));
Usually this is no problem, because (in a clean design) usually all values are of the same type you know before you call in_array()
and thus you can avoid calling it with mismatching types.
This happens because the string 'zebra'
is non-empty and a non-empty string other than '0'
is interpreted by PHP as true
and since there is a matching value in your array you get true
as result.
PHP did a conversion from string to boolean. To avoid this conversion you need to pass a third argument as true
to in_array
:
var_dump(in_array('zebra', $data, true));
"It's not a bug, it's a feature!" =)
Try doing in_array("zebra", $data, true);
which will force "strict" checking (i.e. it will do a type-check on your variables).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With