我有一些代码,它接受用户输入的值和MySql表中的一列,我需要它来运行百分比比较并返回百分比,最高,第二,最高,等等......
这是我目前使用的代码:
<?php
$result_while = mysql_query("SELECT name FROM store_table");
$array = array();
while ($row = mysql_fetch_array($result_while)) {
$array[] = $row[name];
}
$words = $array;
$string2= "Mr Peter Smith";
foreach ($words as $word) {
$percent = (1 - levenshtein($word, $string2)/max( strlen($word),strlen($string2) ) ) * 100;
$word . " - Percent match is: " . $percent[$word] . " </br>";
//60%
}
function compareStrings($s1, $s2) {
//one is empty, so no result
if (strlen($s1)==0 || strlen($s2)==0) {
return 0;
}
//replace none alphanumeric charactors
//i left - in case its used to combine words
$s1clean = preg_replace("/[^A-Za-z0-9-]/", ' ', $s1);
$s2clean = preg_replace("/[^A-Za-z0-9-]/", ' ', $s2);
//remove double spaces
while (strpos($s1clean, " ")!==false) {
$s1clean = str_replace(" ", " ", $s1clean);
}
while (strpos($s2clean, " ")!==false) {
$s2clean = str_replace(" ", " ", $s2clean);
}
//create arrays
$ar1 = explode(" ",$s1clean);
$ar2 = explode(" ",$s2clean);
$l1 = count($ar1);
$l2 = count($ar2);
//flip the arrays if needed so ar1 is always largest.
if ($l2>$l1) {
$t = $ar2;
$ar2 = $ar1;
$ar1 = $t;
}
//flip array 2, to make the words the keys
$ar2 = array_flip($ar2);
$maxwords = max($l1, $l2);
$matches = 0;
//find matching words
foreach($ar1 as $word) {
if (array_key_exists($word, $ar2))
$matches++;
}
return ($matches / $maxwords) * 100;
}
?>
这很好用,并给我一个很长的列表,列出了它们旁边的所有名称及其百分比值。我需要的是最高的,然后按顺序......
如果有意义的话,我需要将范围输出到选择下拉框.....还能够选择最高的输出显示在我页面的单独行中.....
提前致谢。
达米安
答案 0 :(得分:0)
尝试制作此行:
array (
[#SALDO] => Array
(
[1220] => Array
(
[#UB] => Array
(
[-1] => 90000.00
[0] => 10000.00
)
)
[1229] => Array
(
[#UB] => Array
(
[-1] => -20000
[0] => -18000
)
)
)
此:
foreach ($words as $word) {
$percent = (1 - levenshtein($word, $string2)/max(strlen($word),strlen($string2) ) ) * 100;
$word . " - Percent match is: " . $percent[$word] . " </br>";
//60%
}
然后将数组从最大到最小排序:
foreach ($words as $word) {
$percent[$word] = (1 - levenshtein($word, $string2)/max( strlen($word),strlen($string2) ) ) * 100;
}
然后回复:
$percent = arsort($percent);