-1

I have a form that compares 1 word with many and outputs a list of levenshtein scores. How can I get those scores so they list in order, smallest levenshtein score 1st:

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

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

?>

The list outputted would be like this:

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

I want it to be like this:

apple/apes: 3
apple/mango: 5
apple/peach: 5
apple/toothpaste: 8
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
user1721230
  • 317
  • 1
  • 6
  • 19

1 Answers1

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);
}
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • that wierd it works for me to with $string5 = 'carrrot'; then breaks with $string5 = $_POST["singleword"]; – user1721230 Oct 04 '12 at 22:36
  • is it the comma? Sorry my fault!!! Your codes perfect I wasnt inputting commas in my list just using a space. Thank you so very much for your time – user1721230 Oct 04 '12 at 22:40