I have an array with filenames.
I want to check if the array have a file with extension '.txt'.
How can I do that?
in_array
only checks for a specific value.
$iHaz = FALSE;
foreach ($arr as $item) {
if (preg_match('/\.txt$/', $item)) {
$iHaz = TRUE;
break;
}
}
Contrarily to other answers suggesting array_filter
, I don't return something. I just check if it exists in the array. Besides, this implementation is more efficient than array_filter
because it breaks out of the loop once it's found something.