-1

我有一个表格,可以将 1 个单词与多个单词进行比较,并输出一个 levenshtein 分数列表。我怎样才能得到这些分数,以便它们按顺序列出,最小的 levenshtein 分数第一:

<?php
$string5 = $_POST["singleword"];
$string6 = $_POST["manywords"];
$array6 = explode(', ',$string6); 

foreach ($array6 as $derp)
{
    echo $string5, "/", $derp, ": ", levenshtein($string5, $derp), "<br>";
}

?>

输出的列表是这样的:

apple/mango: 5
apple/peach: 5
apple/toothpaste: 8
apple/apes: 3

我希望它是这样的:

apple/apes: 3
apple/mango: 5
apple/peach: 5
apple/toothpaste: 8
4

1 回答 1

2
$string5 = $_POST["singleword"];
$string6 = $_POST["manywords"];

$words = array_flip(array_map('trim', explode(',', $string6)));

foreach ($words as $key => $value)
{
    $words[$key] = levenshtein($string5, $key);
}

asort($words);

foreach ($words as $key => $value)
{
    echo sprintf('%s / %s: %s<br />', $string5, $key, $value);
}
于 2012-10-04T22:17:00.897 回答