Skip to content

Instantly share code, notes, and snippets.

@Mika-
Last active December 17, 2020 13:36
Show Gist options
  • Save Mika-/00c869f1fef0bc7e3f42679323bf7b99 to your computer and use it in GitHub Desktop.
Save Mika-/00c869f1fef0bc7e3f42679323bf7b99 to your computer and use it in GitHub Desktop.
<?php
/**
* Find first element from array by callback
*
* @param array|\Traversable $array
* @param callable $callback
* @return mixed|null
*/
function array_find($array, callable $callback)
{
foreach ($array as $key => $value) {
$result = $callback($value, $key, $array);
if ($result) {
return $value;
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment