note 124961 added to function.array-search
| From: | yasien dot dwieb at gmail dot com | Date: | Tue, 28 Apr 2020 17:41:00 +0000 |
| Subject: | note 124961 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214557@lists.php.net to get a copy of this message | ||
Beware when using array_search to a mix of string and integer where prefixes of keys may collide, as
in my case I have encountered the following situation:
Assume you have the following array:
<?php
$arr = [
1 => 'index 0',
2 => 'index 1',
3 => 'index 2',
'3anothersuffix' => 'index 3'
];
$index1 = array_search('3', array_keys($arr)); // 2
$index2 = array_search('3anothersuffix', array_keys($arr)); //2
?>
$index1 and $index2 will be the same
after using strict type search:
<?php
$index1 = array_search('3', array_keys($arr), true); // false
$index2 = array_search('3anothersuffix', array_keys($arr), true); //3
?>
it will not find $index1 at all while returning a correct value for $index2;
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.195 (proxied: 197.32.12.217)
----
Manual Page -- http://php.net/manual/en/function.array-search.php
Edit -- https://master.php.net/note/edit/124961
Del: integrated -- https://master.php.net/note/delete/124961/integrated
Del: useless -- https://master.php.net/note/delete/124961/useless
Del: bad code -- https://master.php.net/note/delete/124961/bad+code
Del: spam -- https://master.php.net/note/delete/124961/spam
Del: non-english -- https://master.php.net/note/delete/124961/non-english
Del: in docs -- https://master.php.net/note/delete/124961/in+docs
Del: other reasons-- https://master.php.net/note/delete/124961
Reject -- https://master.php.net/note/reject/124961
Search -- https://master.php.net/manage/user-notes.php