$arr = array(
'test' => array(
'soap' => true,
),
);
$input = 'hey';
if (in_array($input, $arr['test'])) {
echo $input . ' is apparently in the array?';
}
Result:
hey is apparently in the array?
It doesn’t make any sense to me, please explain why. And how do I fix this?
That’s because
true == 'hey'due to type juggling. What you’re looking for is:It forces an equality test based on
===instead of==.To understand type juggling better you can play with this:
Update
If you want to know if an array key is set (rather than if a value is present), you should use
isset()like this:Update 2
There’s also
array_key_exists()that can test for array key presence; however, it should only be used if there’s a possibility that the corresponding array value can benull.