note 97600 added to function.array-search

From: Date: Tue, 27 Apr 2010 16:38:46 +0000
Subject: note 97600 added to function.array-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-168729@lists.php.net to get a copy of this message
Mrjay42, you are assuming that your associative array's keys are integers. You can simplify your function, and it will work just the same without the counters. It's also important to return a false if you couldn't find a key, as your function may return a result that could be interpreted as false (like 0). So just use it with === operator, or !== operator. function array_key_search($needle, $haystack){ foreach($haystack as $key => $value){ if($key == $needle){ return $key; } } return false; } Thanks... ---- Server IP: 216.235.13.26 Probable Submitter: 216.185.76.77 ---- Manual Page -- http://www.php.net/manual/en/function.array-search.php Edit -- https://master.php.net/note/edit/97600 Del: integrated -- https://master.php.net/note/delete/97600/integrated Del: useless -- https://master.php.net/note/delete/97600/useless Del: bad code -- https://master.php.net/note/delete/97600/bad+code Del: spam -- https://master.php.net/note/delete/97600/spam Del: non-english -- https://master.php.net/note/delete/97600/non-english Del: in docs -- https://master.php.net/note/delete/97600/in+docs Del: other reasons-- https://master.php.net/note/delete/97600 Reject -- https://master.php.net/note/reject/97600 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168729) next »