Created
March 18, 2013 01:05
-
-
Save danielmartins/5184368 to your computer and use it in GitHub Desktop.
PHP: Remove duplicates items of an array based on similarities
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
<?php | |
/** | |
* This functions runs like array_unique but is based on similarity | |
* and the math is based on similar_text to know better, see: php.net/similar_text | |
* | |
* @param [type] $array [description] | |
* @param integer $howMuch [description] | |
* @param [type] $callback [description] | |
* @return [type] [description] | |
*/ | |
function array_remove_similar($array, $howMuch = 90, $callback = null) | |
{ | |
// clone array | |
$newArr = array_merge(array(), $array); | |
$similars = function($needle, $haystack, $key2ignore) use ($howMuch) { | |
$keys = []; | |
foreach ($haystack as $key => $value) | |
{ | |
if($key === $key2ignore) | |
continue; | |
similar_text($needle, $value, $p); | |
if($p >= $howMuch) | |
$keys[] = $key; | |
} | |
return $keys; | |
}; | |
foreach ($array as $key => $value) | |
{ | |
$keys = $similars($value, $array, $key); | |
foreach ($keys as $k) { | |
unset($newArr[$k]); | |
} | |
} | |
return $newArr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment