Prabhath Karunakalage
Prabhath Karunakalage

Reputation: 67

sort php array by relevance

I want to sort a php array in a way that it compares to a certain string and

  1. Exact match of the string is in the top
  2. the rest need to be sorted according to relevance to the string. The most similar string to be next and so on.

I am getting the array remotely so i do not have any control over sql statements
I have googled and couldn't find a satisfying answer.
Thank you in advance

Upvotes: 2

Views: 1668

Answers (1)

Benjamin Paap
Benjamin Paap

Reputation: 2759

You could use usort with a callback function that utilizes levenshtein() to sort your array.

Upvotes: 5

Related Questions