note 93833 added to function.array-search

From: Date: Fri, 02 Oct 2009 07:22:13 +0000
Subject: note 93833 added to function.array-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-161214@lists.php.net to get a copy of this message
one thing to be very aware of is that array_search() will fail if the needle is a string and the array itself contains values that are mixture of numbers and strings. (or even a string that looks like a number) The problem is that unless you specify "strict" the match is done using == and in that case any string will match a numeric value of zero which is not what you want. ----- also, php can lookup an index pretty darn fast. for many scenarios, it is practical to maintain multiple arrays, one in which the index of the array is the search key and the normal array that contains the data. <php $normal[$index] = array('key'=>$key, 'data'=>'foo'); $inverse[$key] = $index; //very fast lookup, this beats any other kind of search if (array_key_exists($key, $inverse)) { $index = $inverse[$key]; return $normal[$index]; } ?> ---- Server IP: 216.235.13.26 Probable Submitter: 174.21.165.151 ---- Manual Page -- http://www.php.net/manual/en/function.array-search.php Edit -- https://master.php.net/note/edit/93833 Del: integrated -- https://master.php.net/note/delete/93833/integrated Del: useless -- https://master.php.net/note/delete/93833/useless Del: bad code -- https://master.php.net/note/delete/93833/bad+code Del: spam -- https://master.php.net/note/delete/93833/spam Del: non-english -- https://master.php.net/note/delete/93833/non-english Del: in docs -- https://master.php.net/note/delete/93833/in+docs Del: other reasons-- https://master.php.net/note/delete/93833 Reject -- https://master.php.net/note/reject/93833 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#161214) next »