note 99097 added to function.array-search
| From: | infinitev at hotmail dot com | Date: | Tue, 27 Jul 2010 12:25:17 +0000 |
| Subject: | note 99097 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-171170@lists.php.net to get a copy of this message | ||
if your looking for fast and simple to use array_search that can search both keys and values..
try mine :)
static function key_value_array_search($value, $array, $is_key=FALSE) {
$result = FALSE;
if(!$is_key) $result = array_search($value, $array);
else $result = (in_array($value, array_keys($array)) ? $array[$value] : FALSE);
return $result;
}
hope this helps someone, some of the others seems well over complex :/
example:
//test array
$arr = array('fruit' => 'banana', 'number' => 5,
'sport' => 'football', 8 => 'numeric');
$result = key_value_array_search(5, $arr, FALSE);
echo $result."\n\n";
$result = key_value_array_search('fruit', $arr, TRUE);
echo $result;
results:
number
banana
regards,
Sean.
----
Server IP: 69.147.83.197
Probable Submitter: 211.30.172.123
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- https://master.php.net/note/edit/99097
Del: integrated -- https://master.php.net/note/delete/99097/integrated
Del: useless -- https://master.php.net/note/delete/99097/useless
Del: bad code -- https://master.php.net/note/delete/99097/bad+code
Del: spam -- https://master.php.net/note/delete/99097/spam
Del: non-english -- https://master.php.net/note/delete/99097/non-english
Del: in docs -- https://master.php.net/note/delete/99097/in+docs
Del: other reasons-- https://master.php.net/note/delete/99097
Reject -- https://master.php.net/note/reject/99097
Search -- https://master.php.net/manage/user-notes.php