문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top