<?php
function in_arrayi($needle, array $haystack, bool $strict = false): bool
{
if (!is_scalar($needle)) {
// throw exception or something because of unsuitable data type
}
$needle = strtolower($needle);
foreach ($haystack as $value) {
if (!is_scalar($value)) {
continue;
} elseif ($strict && strtolower($value) === $needle) {
return true;
} elseif (!$strict && strtolower($value) == $needle) {
return true;
}
}
return false;
}
$haystack = array('one', 'two', 'three', 'four');
$test_needles = ['onE', 'tWo', 'THREE', 'four', 'five'];
foreach ($test_needles as $needle) {
echo $needle . ': ' . (in_arrayi($needle, $haystack) ? 'found' : 'missing') . "\n";
}
preferences:
27.33 ms | 402 KiB | 5 Q