» Birthday Bundle - Over $400 worth of Envato files for just $20

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!

Get our RSS Feed!

Leave a Reply


* = required fields

  (will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: If you want to post CODE Snippets, please make them postable first!
(e.g. <br /> should be converted to &lt;br /&gt;)