note 18288 deleted from function.array-search by didou
| From: | didou@php.net | Date: | Mon, 15 Dec 2003 18:32:16 +0000 |
| Subject: | note 18288 deleted from function.array-search by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-62000@lists.php.net to get a copy of this message | ||
Note Submitter: nospamhere@vt.edu
----
> $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.