470,870 Members | 1,418 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

in_array oddity

Check out this code:

// Start Code -------------
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???
Jul 17 '05 #1
3 2096
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)
Jul 17 '05 #2
Tom Barnes wrote:
Check out this code:

// Start Code -------------
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???

Wrong question! The right question is:

"Why does the first in_array() call return true?"
And the answer is:

Because 'key' is converted to numeric, to 0 (zero) and 0 (zero) *is* in
the $a array.

The second in_array() call tries to find a 'key' (or 0) but fails
because the value in the array is 1.


Try specifying the third parameter to the in_array() call ...

in_array('key', $a, true)

Happy Coding :-)

--
USENET would be a better place if everybody read:
http://www.expita.com/nomime.html
http://www.netmeister.org/news/learn2quote2.html
http://www.catb.org/~esr/faqs/smart-questions.html
Jul 17 '05 #3
no********@yahoo.com (Tom Barnes) wrote in message news:<5a*************************@posting.google.c om>...
Check out this code:

// Start Code -------------
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???


I'm so stupid, for some reason I thought in_array() was searching for
keys. I should use array_key_exists() instead. Thanks Brion and Pedro.
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mountain Man | last post: by
3 posts views Thread by Phil Powell | last post: by
6 posts views Thread by Craig Keightley | last post: by
5 posts views Thread by Phil Powell | last post: by
4 posts views Thread by paul brown | last post: by
6 posts views Thread by Berimor | last post: by
3 posts views Thread by Sonnich | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.