note 88465 added to function.array-search

From: Date: Sun, 25 Jan 2009 21:05:32 +0000
Subject: note 88465 added to function.array-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-149763@lists.php.net to get a copy of this message
I was trying to use array_search to retrieve all the values that match a given needle, but it turns out only the first match key is returned. I built this little function, which works just like array_search, but returns all the keys that match a given needle instead. The output is an array. <?php $haystack = array('a','b','a','b'); $needle = 'a'; print_r(array_search_all($needle, $haystack)); //Output will be // Array // ( // [0]=>1 // [1]=>3 // ) function array_search_all($needle, $haystack) {#array_search_match($needle, $haystack) returns all the keys of the values that match $needle in $haystack foreach ($haystack as $k=>$v) { if($haystack[$k]==$needle){ $array[] = $k; } } return ($array); } ?> ---- Server IP: 209.41.74.194 Probable Submitter: 98.194.204.33 ---- Manual Page -- http://www.php.net/manual/en/function.array-search.php Edit -- https://master.php.net/note/edit/88465 Del: integrated -- https://master.php.net/note/delete/88465/integrated Del: useless -- https://master.php.net/note/delete/88465/useless Del: bad code -- https://master.php.net/note/delete/88465/bad+code Del: spam -- https://master.php.net/note/delete/88465/spam Del: non-english -- https://master.php.net/note/delete/88465/non-english Del: in docs -- https://master.php.net/note/delete/88465/in+docs Del: other reasons-- https://master.php.net/note/delete/88465 Reject -- https://master.php.net/note/reject/88465 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#149763) next »