Skip to content

Instantly share code, notes, and snippets.

@pbroschwitz
Created July 9, 2013 10:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbroschwitz/5956354 to your computer and use it in GitHub Desktop.
Save pbroschwitz/5956354 to your computer and use it in GitHub Desktop.
/**
* This is similar to http://php.net/manual/en/function.array-reduce.php but returns
* the return from the callback immediately when it is 'truthy'. The callback also
* is being passed the 'key' in addition to just the 'value'. So this is basically
* some sort of more flexible array_reduce, array_filter and array_search
*/
function array_find($array, $callback) {
foreach ($array as $key => $value) {
$result = $callback($value, $key);
if ($result) {
return $result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment