note 69588 added to function.array-search
| From: | mark dot php at mhudson dot net | Date: | Tue, 12 Sep 2006 00:49:01 +0000 |
| Subject: | note 69588 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-117038@lists.php.net to get a copy of this message | ||
I was going to complain bitterly about array_search() using zero-based indexes, but then I realized
I should be using in_array() instead.
// if ( isset( $_GET['table'] ) and array_search( $_GET['table'], $valid_tables)
) { // BAD: fails on first[0] element
// if ( isset( $_GET['table'] ) and ( FALSE !== array_search( $_GET['table'],
$valid_tables) ) ) { OK: but wasteful and convoluted
if ( isset( $_GET['table'] ) and in_array( $_GET['table'], $valid_tables) ) {
// BETTER
The essence is this: if you really want to know the location of an element in an array, then use
array_search, else if you only want to know whether that element exists, then use in_array()
----
Server IP: 64.71.164.2
Probable Submitter: 65.88.178.10
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- https://master.php.net/note/edit/69588
Del: integrated -- https://master.php.net/note/delete/69588/integrated
Del: useless -- https://master.php.net/note/delete/69588/useless
Del: bad code -- https://master.php.net/note/delete/69588/bad+code
Del: spam -- https://master.php.net/note/delete/69588/spam
Del: non-english -- https://master.php.net/note/delete/69588/non-english
Del: in docs -- https://master.php.net/note/delete/69588/in+docs
Del: other reasons-- https://master.php.net/note/delete/69588
Reject -- https://master.php.net/note/reject/69588
Search -- https://master.php.net/manage/user-notes.php