Question

How can I order an array of results by how much the array's keys is like a specific string. If I have a string like this:

Hello World

And an array:

array(
    [0] => "hello word"
    [1] => "herro world"
    [2] => "beho wors"

I tried using it with the Levenshtein function, but I couldn't get to understand how I could do it. I'm new to PHP.

Was it helpful?

Solution

Use usort() (http://us2.php.net/manual/en/function.usort.php), and have your compare function use the Levenshtein distance.

If that value is expensive to compute, then pre-compute it and store it in a separate array so that your compare function doesn't have to recompute it frequently during the sort.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top