Creating the array_isearch() Function
Posted on November 8, 2008, Filled under PHP,
Bookmark it
Hello coders,
We know that arrray_search() is a function used to search the array for given value and return the corresponding key. This works fine in a case sensitive mode, but in an insensitive one we have a problem. If we search for ‘blue’ and there is a value in array ‘BLUE’, our array_search() will return NULL. Here’s a function that works like array_search() & ignores the sensitive case:
<?php
function array_isearch($value, $array)
{
while (list($key, $val) = each($array))
{
$val = strtolower($val);
$value = strtolower($value);
if($val == $value) return $key;
}
return false;
}
?>
Usage Example:
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key_one = array_isearch('BLUE', $array);
echo 'Key One: '.$key_one;
echo '<br />';
$key_two = array_isearch('GrEeN', $array);
echo 'Key Two: '.$key_two;
?>
Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!
- November 8, 2008
- article by Gabriel C.
- Leave a reply!
Related Posts
Equivalent of PHP’s array_combine() functionat October 15, 2008
PHP: Make an Alphabetical Selection from Elements of an Arrayat October 5, 2008
PHP: Usage of Range() Function (Alphabetical & Numerical Output)at September 2, 2008
PHP: Equivalent of trim() Function for Arraysat October 5, 2008 with 2 comments
Equivalent of PHP’s in_array() functionat September 13, 2008 with 6 comments
