note 18288 added to function.array-search
| From: | nospamhere at vt dot edu | Date: | Thu, 17 Jan 2002 08:17:51 +0000 |
| Subject: | note 18288 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-24970@lists.php.net to get a copy of this message | ||
> $map = array( 'version' => 4
> , 'OS' => 'Linux'
> , 'lang' => 'english'
> , 'short_tags' => true
> );
> $key = array_search("4",$map);
> print $key;
> //returns short_tags
> //strange results..
That's true, because when you're comparing the string "4" to the boolean value
true, the string is transparently cast to a boolean. So in this case, "4" matches two of
the elements of the array. When that happens, it seems that this function always returns the
"last" one that matches, i.e. the one that was added to the array last.
If you instead do
$key = array_search("4",$map,true);
then you still get a false return value, because the string "4" doesn't match the
boolean or the integer in the array.
But if you do
$key = array_search(4,$map,true);
then it works as expected. Note the lack of quotes on the 4, and note that you still need the
optional third parameter ("strict") here.
--
http://www.php.net/manual/en/function.array-search.php
http://master.php.net/manage/user-notes.php?action=edit+18288
http://master.php.net/manage/user-notes.php?action=delete+18288
http://master.php.net/manage/user-notes.php?action=reject+18288