v-bartusevicius/levenshtein-array-sort

根据 Levenshtein 距离对字符串数组进行排序

dev-master / 0.1.x-dev 2015-03-07 13:40 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:46:35 UTC


README

按 Levenshtein 距离对给定的字符串数组进行排序。

简单使用

$needle = 'text';
$haystack = array('texas', 'test', 'random');

$sorter = new ArraySort();
$result = $sorter->sort($needle, $haystack);

print_r($result);

结果将表示为按 Levenshtein 距离到针的顺序排列的数组,键为 Levenshtein 距离

Array
(
    [1] => test
    [2] => texas
    [6] => random
)

如果到针的距离相同,则将创建嵌套数组

$needle = 'text';
$haystack = array('texas', 'test', 'texts', 'random');

$result = $sorter->sort($needle, $haystack);

print_r($result);
Array
(
    [1] => Array
        (
            [0] => test
            [1] => texts
        )

    [2] => texas
    [6] => random
)

安装: composer require v-bartusevicius/levenshtein-array-sort