Thread: in_array oddity
View Single Post

  #2 (permalink)  
Old 10-12-2004
Brion Vibber
 
Posts: n/a
Default Re: in_array oddity

Tom Barnes wrote:
> function test_in_array($val)
> {
> $a = array('key' => $val);
> printf("in_array: %d, value:%s<BR>", in_array('key', $a), $a['key']);
> }
> test_in_array(0);
> test_in_array(1);
> // End Code ---------------
>
> The output I get is:
>
> in_array: 1, value:0
> in_array: 0, value:1
>
> Why does the second in_array() call fail???


Well, the second call returns false because 'key' does not appear as a
value in the associative array. (Only as a key.) It also returns false
for values of 2, 3, etc.

I have no idea why the first call returns true, though... If you pass
true as the third parameter ("strict") to in_array, it will return false
as expected.

-- brion vibber (brion @ pobox.com)
Reply With Quote