PHP in_array() Case Insensitive

A very nifty function to carry out a case insensitive in_array() search:



function in_array_case_insensitive($needle, $haystack)
{
return in_array( strtolower($needle), array_map('strtolower', $haystack) );
}



Easy, huh? Here's an example of it in use




$fileExtensionsArray = Array("php", "html", "ASP", "sql", "JSP");
$extension = "PHP";

if (in_array_case_insensitive($extension, $fileExtensionsArray))
{
echo "Yes, $extension is in array";
}
else
{
echo "No $extension is NOT in array";
}


8 comments:

  1. Thanks for this! Didn't realise it was so easy to implement this.

    ReplyDelete
  2. Thanks for this piece of code.. saved lot of time :)

    ReplyDelete
  3. Very clever, helpt me alot, Thanks

    ReplyDelete
  4. I am not sure where you are getting your info, but great topic.
    I needs to spend some time learning more or understanding more.

    Thanks for magnificent info I was looking for this information for
    my mission.

    Here is my page; magento themes

    ReplyDelete
  5. Excellent piece of code. Thank you!

    ReplyDelete
  6. Works great! Thanks

    ReplyDelete
  7. What if $haystack is array of array.?

    ReplyDelete
  8. This is really Nice Information you shared with us appreciate it

    ReplyDelete