note 89191 added to function.array-key-exists
| From: | GibsonSG at osu1 dot php dot net | Date: | Wed, 25 Feb 2009 23:55:08 +0000 |
| Subject: | note 89191 added to function.array-key-exists | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-151138@lists.php.net to get a copy of this message | ||
Below is a more efficient and useful case-insensitive array_key_exists function. More useful in the
sense that it returns the matching key.
/**
* Case insensitive version of array_key_exists.
* Returns the matching key on success, else false.
*
* @param string $key
* @param array $search
* @return string|false
*/
function array_key_exists_nc($key, $search) {
if (array_key_exists($key, $search)) {
return $key;
}
if (!(is_string($key) && is_array($search) && count($search))) {
return false;
}
$key = strtolower($key);
foreach ($search as $k => $v) {
if (strtolower($k) == $key) {
return $k;
}
}
return false;
}
/* Test code: */
$array = array(
'taste' => 'sweet',
'cOlOr' => 'yellow',
'foo',
'bar',
);
$keys = array(
'Color',
'google',
0,
);
foreach ($keys as $key) {
print "\t$key: "; var_dump(array_key_exists_nc($key, $array));
}
/* Output:
Color: string(5) "cOlOr"
google: bool(false)
0: int(0)
*/
----
Server IP: 83.137.20.135
Probable Submitter: 80.101.188.146
----
Manual Page -- http://www.php.net/manual/en/function.array-key-exists.php
Edit -- https://master.php.net/note/edit/89191
Del: integrated -- https://master.php.net/note/delete/89191/integrated
Del: useless -- https://master.php.net/note/delete/89191/useless
Del: bad code -- https://master.php.net/note/delete/89191/bad+code
Del: spam -- https://master.php.net/note/delete/89191/spam
Del: non-english -- https://master.php.net/note/delete/89191/non-english
Del: in docs -- https://master.php.net/note/delete/89191/in+docs
Del: other reasons-- https://master.php.net/note/delete/89191
Reject -- https://master.php.net/note/reject/89191
Search -- https://master.php.net/manage/user-notes.php