Biz diziler yalnızca dizeleri içerebilir varsayalım eğer yukarıdakiler doğru ise, ancak diziler de başka diziler içerebilir. Ayrıca in_array () fonksiyonu $ iğne için bir dizi kabul edebilir, böylece $ iğne bir dizi olup olmadığını strtolower ($ iğne) işe gitmiyor ve (, 'strtolower' $ samanlık) array_map $ samanlık içeriyorsa işe gitmiyor, diğer diziler, ama ": strtolower () parametre 1 verilen dize, dizi olmasını beklediğini PHP uyarı" neden olur.
Example:
$needle = array('p', 'H');
$haystack = array(array('p', 'H'), 'U');
Yani ben harf duyarlıdır ve harf duyarsız in_array () kontrolleri yapmak, releveant yöntemleri ile yardımcı sınıf yarattı. Ben de yerine strtolower (of mb_strtolower ()) kullanıyorum, böylece diğer kodlamalar kullanılabilir. İşte kod:
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':
switch (true) {
case is_string($needle):
return in_array(self::toLower($needle), self::arrayToLower($haystack), $strict);
break;
case is_array($needle):
return in_array(self::arrayToLower($needle), self::arrayToLower($haystack), $strict);
break;
}
break;
}
}
}