- Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
- 0
<?php
$haystack = "I like cats.";
$needles = ['cat' => 0, 'dog' => 1];
function inefficient(string $haystack, array $needles) {
$qualifiers = array_filter(
$needles,
fn($k) => strpos($haystack, $k) !== false,
ARRAY_FILTER_USE_KEY
);
if (!$qualifiers) {
throw new Exception('No needles were found in the haystack');
}
return current($qualifiers);
}
try {
var_export(inefficient($haystack, $needles));
} catch (Exception $e) {
echo $e->getMessage();
}