note 116580 added to function.in-array
| From: | HarryWillis | Date: | Sat, 24 Jan 2015 11:39:23 +0000 |
| Subject: | note 116580 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201516@lists.php.net to get a copy of this message | ||
Kelvin's case-insensitive in_arrayi is fine if you desire loose typing, but mapping strtolower
onto the array will (attempt to) cast all array members to string. If you have an array of mixed
types, and you wish to preserve the typing, the following will work:
<?php
function in_array_ci($needle, array $haystack, $strict = false) {
foreach ($haystack as $element) {
if (gettype($needle) == 'string' && gettype($element) ==
'string') {
if (!strcasecmp($needle, $element)) {
return true;
}
}
elseif ($strict) {
if ($needle === $element) {
return true;
}
}
else {
if ($needle == $element) {
return true;
}
}
}
return false;
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 81.110.85.173
----
Manual Page -- http://php.net/manual/en/function.in-array.php
Edit -- https://master.php.net/note/edit/116580
Del: integrated -- https://master.php.net/note/delete/116580/integrated
Del: useless -- https://master.php.net/note/delete/116580/useless
Del: bad code -- https://master.php.net/note/delete/116580/bad+code
Del: spam -- https://master.php.net/note/delete/116580/spam
Del: non-english -- https://master.php.net/note/delete/116580/non-english
Del: in docs -- https://master.php.net/note/delete/116580/in+docs
Del: other reasons-- https://master.php.net/note/delete/116580
Reject -- https://master.php.net/note/reject/116580
Search -- https://master.php.net/manage/user-notes.php