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:
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.