Created
August 28, 2015 12:48
-
-
Save firomero/175b0ed72136b5000269 to your computer and use it in GitHub Desktop.
array_unique_callback
This file contains 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
/** | |
* 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