note 123310 deleted from function.array-search by crell
| From: | crell@php.net | Date: | Thu, 31 Mar 2022 20:37:48 +0000 |
| Subject: | note 123310 deleted from function.array-search by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217947@lists.php.net to get a copy of this message | ||
Note Submitter: venkatesh dot ksnadar at gmail dot com
----
you need to be careful if you are using array_search() with unset() for deleting a value in the
searched indexed
// wrong way
$checked = ["1", "2", "3"]; // array with numbers in double quotes
unset($checked[array_search('on', $checked)]); // this deletes value at 0 index
// array_search returns mixed data type, which means any type of data will be returned
// in the above case it returned false and false is type casted to 0
// so it deletes the value at index zero
the point to say is never use array_search() with unset() while deleting a value in an array using
index.