Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stemwinder/40e5015d9c2e671213617aefa389e093 to your computer and use it in GitHub Desktop.
Save stemwinder/40e5015d9c2e671213617aefa389e093 to your computer and use it in GitHub Desktop.
PHP: Remove duplicates items of an array based on similarities
<?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