The algorithm itself works great, do not touch it.
However, you can add some feeds by writing a generic search function:
function array_find(array $a, callable $fn)
{
foreach ($a as $key => $value) {
if ($fn($value, $key, $a)) {
return $key;
}
}
return false;
}
$key = array_find([0, 0, 4, 4, 5, 7], function($value) {
return $value >= 5;
});
Now, although this is a more elegant approach, it is less effective; There is significant overhead that caused the closure of each item. If performance is paramount, use what you have and run with it.
Ja͢ck source
share