in_array oddity

This is a discussion on in_array oddity within the PHP Language forums, part of the PHP Programming Forums category; Check out this code: // Start Code ------------- function test_in_array($val) { $a = array('key' => $val); printf("in_array: %d, value:%s&...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-12-2004
Tom Barnes
 
Posts: n/a
Default 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???
Reply With Quote
  #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
  #3 (permalink)  
Old 10-12-2004
Pedro Graca
 
Posts: n/a
Default Re: in_array oddity

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
Reply With Quote
  #4 (permalink)  
Old 10-12-2004
Tom Barnes
 
Posts: n/a
Default Re: in_array oddity

nospam1978@yahoo.com (Tom Barnes) wrote in message news:<5af28966.0410111503.f6f243f@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.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:37 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0