Skip to content

Instantly share code, notes, and snippets.

@firomero
Created August 28, 2015 12:48
Show Gist options
  • Save firomero/175b0ed72136b5000269 to your computer and use it in GitHub Desktop.
Save firomero/175b0ed72136b5000269 to your computer and use it in GitHub Desktop.
array_unique_callback
/**
* Array unique callback
* @param array $arr
* @param callable $callback
* @param bool $strict
* @return array
*/
function array_unique_callback(array $arr, callable $callback, $strict = false)
{
return array_filter(
$arr,
function ($item) use ($strict, $callback) {
static $haystack = array();
$needle = $callback($item);
if (in_array($needle, $haystack, $strict)) {
return false;
} else {
$haystack[] = $needle;
return true;
}
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment