基于input-php命令数组

基于input-php命令数组,php,arrays,sorting,Php,Arrays,Sorting,在PHP中,有没有办法创建一个数组,并按照最类似于某个字符串的顺序对其排序 例如: $array = array("Bob", "Brad", "Britney"); $userinput = "Bradley123"; /* function that changes the array to be array("Brad","Britney","Bob") from the most similar from the $userinput */ 我做了一些研究,这里有一种方法可以做到:

PHP
中,有没有办法创建一个数组,并按照最类似于某个字符串的顺序对其排序

例如:

$array = array("Bob", "Brad", "Britney");
$userinput = "Bradley123";

/*
function that changes the array to be
array("Brad","Britney","Bob")
from the most similar from the $userinput
*/

我做了一些研究,这里有一种方法可以做到:

$array = array(0 => 'blue', 1 => 'reds', 2 => 'green', 3 => 'reds'); 
//Words to be searched from.

$res = array("percent" => "0", "word" => "N/A");
//Result, this is an array for the bellow loop to talk to.

foreach($array AS $ar){
  similar_text("red", $ar, $percent);

  //If it's more like the word, then replace the percentage with the percentage of similarity & word.
  if($percent > $res["percent"]){
    $res["percent"] = $percent;
    $res["word"] = $ar;
  }
}

//Echo out the most similar word.
echo $res["word"];

据我所知,这是做类似事情的唯一可能的方法。

我认为没有可能的方法。你如何定义“最相似”呢?如果你指的是谷歌之类的东西,试试看,然后你应该使用一个数据库并通过它进行搜索,而不是使用数组。没有内置的方法可以做到这一点。你必须自己编写代码。您可能可以将链接到的
similor\u text
函数u\u mulder与
usort
结合使用。我已尝试修改您的代码,使其所有结果都位于同一数组中,但一直失败。你知道有什么方法可以将每个结果放入数组中,并以
most
类似于
least
similarI的方式对其进行排序吗?我实际上不知道这是否可能,我的意思是,每个循环都会有额外的排序。正如我所说,您可以使用类似于数据库的东西,但是这个脚本已经完成了您刚才要求的任务,但没有将其放入数组中。