Case inSensitive in_array()

You might already know that PHP’s in_array() function matches the string needle case sensitive way. However, today I was needed something that matches in case insensitive way. Before doing it myself, I googled it and I found a ready made solution. It is quite simple. Here it is:

function in_arrayi($needle, $haystack)
{
    foreach ($haystack as $value)
    {
        if (strtolower($value) == strtolower($needle))
        return true;
    }
    return false;
}

Please note that this case-insensitive version is, at least, 5 times slower than in_array().

  • umesh

    you can use this also
    array_walk($arr_url,create_function(‘&$v,$k’,’$v=strtolower($v);’));
    instead of foreach