note 124985 added to function.array-search
| From: | nordseebaer at gmx dot de | Date: | Fri, 08 May 2020 08:08:56 +0000 |
| Subject: | note 124985 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214591@lists.php.net to get a copy of this message | ||
It's really important to check the return value is not false! I used array_search() to
determine the index of an value to unset this value and then realized that $arr[false] === $arr[0] !
<?php
$arr = ['apple', 'banana'];
var_dump($arr[0] === 'apple'); // true
var_dump($arr[false] === $arr[0]); // true
var_dump($arr[false] === 'apple'); // true
unset($arr[array_search('banana', $arr)]); //index = 1
var_dump($arr);
// result
// array(1) {
// [0]=>
// string(5) "apple"
// }
unset($arr[array_search('peach', $arr)]); //not found, result is false
var_dump($arr);
// result
// array(0) {
// }
// because $arr[false] === $arr[0]
?>
So always check the return of array_search!
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.195 (proxied: 62.96.28.222)
----
Manual Page -- http://php.net/manual/en/function.array-search.php
Edit -- https://master.php.net/note/edit/124985
Del: integrated -- https://master.php.net/note/delete/124985/integrated
Del: useless -- https://master.php.net/note/delete/124985/useless
Del: bad code -- https://master.php.net/note/delete/124985/bad+code
Del: spam -- https://master.php.net/note/delete/124985/spam
Del: non-english -- https://master.php.net/note/delete/124985/non-english
Del: in docs -- https://master.php.net/note/delete/124985/in+docs
Del: other reasons-- https://master.php.net/note/delete/124985
Reject -- https://master.php.net/note/reject/124985
Search -- https://master.php.net/manage/user-notes.php