Is it possible to do case-insensitive comparison when using the in_array
function?
So with a source array like this:
$a= array(
\'one\'
I wrote a simple function to check for a insensitive value in an array the code is below.
function:
function in_array_insensitive($needle, $haystack) {
$needle = strtolower($needle);
foreach($haystack as $k => $v) {
$haystack[$k] = strtolower($v);
}
return in_array($needle, $haystack);
}
how to use:
$array = array('one', 'two', 'three', 'four');
var_dump(in_array_insensitive('fOUr', $array));
function in_arrayi($needle, $haystack) {
return in_array(strtolower($needle), array_map('strtolower', $haystack));
}
From Documentation
The above is correct if we assume that arrays can contain only strings, but arrays can contain other arrays as well. Also in_array() function can accept an array for $needle, so strtolower($needle) is not going to work if $needle is an array and array_map('strtolower', $haystack) is not going to work if $haystack contains other arrays, but will result in "PHP warning: strtolower() expects parameter 1 to be string, array given".
Example:
$needle = array('p', 'H');
$haystack = array(array('p', 'H'), 'U');
So i created a helper class with the releveant methods, to make case-sensitive and case-insensitive in_array() checks. I am also using mb_strtolower() instead of strtolower(), so other encodings can be used. Here's the code:
class StringHelper {
public static function toLower($string, $encoding = 'UTF-8')
{
return mb_strtolower($string, $encoding);
}
/**
* Digs into all levels of an array and converts all string values to lowercase
*/
public static function arrayToLower($array)
{
foreach ($array as &$value) {
switch (true) {
case is_string($value):
$value = self::toLower($value);
break;
case is_array($value):
$value = self::arrayToLower($value);
break;
}
}
return $array;
}
/**
* Works like the built-in PHP in_array() function — Checks if a value exists in an array, but
* gives the option to choose how the comparison is done - case-sensitive or case-insensitive
*/
public static function inArray($needle, $haystack, $case = 'case-sensitive', $strict = false)
{
switch ($case) {
default:
case 'case-sensitive':
case 'cs':
return in_array($needle, $haystack, $strict);
break;
case 'case-insensitive':
case 'ci':
if (is_array($needle)) {
return in_array(self::arrayToLower($needle), self::arrayToLower($haystack), $strict);
} else {
return in_array(self::toLower($needle), self::arrayToLower($haystack), $strict);
}
break;
}
}
}
/**
* in_array function variant that performs case-insensitive comparison when needle is a string.
*
* @param mixed $needle
* @param array $haystack
* @param bool $strict
*
* @return bool
*/
function in_arrayi($needle, array $haystack, bool $strict = false): bool
{
if (is_string($needle)) {
$needle = strtolower($needle);
foreach ($haystack as $value) {
if (is_string($value)) {
if (strtolower($value) === $needle) {
return true;
}
}
}
return false;
}
return in_array($needle, $haystack, $strict);
}
/**
* in_array function variant that performs case-insensitive comparison when needle is a string.
* Multibyte version.
*
* @param mixed $needle
* @param array $haystack
* @param bool $strict
* @param string|null $encoding
*
* @return bool
*/
function mb_in_arrayi($needle, array $haystack, bool $strict = false, ?string $encoding = null): bool
{
if (null === $encoding) {
$encoding = mb_internal_encoding();
}
if (is_string($needle)) {
$needle = mb_strtolower($needle, $encoding);
foreach ($haystack as $value) {
if (is_string($value)) {
if (mb_strtolower($value, $encoding) === $needle) {
return true;
}
}
}
return false;
}
return in_array($needle, $haystack, $strict);
}
$a = array( 'one', 'two', 'three', 'four' );
$b = in_array( 'ONE', $a, false );