Created
July 9, 2013 10:29
-
-
Save pbroschwitz/5956354 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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