2 回答

TA貢獻1799條經驗 獲得超9個贊
您可以使用 Php 函數levenshtein來衡量您的搜索詞與給定字符串的相似程度。對于多維數組中的每個項目,您可以使用 levenshtein 函數計算相似度。相似度可以與原始字符串一起存儲在一個數組中。然后可以對數組進行排序以獲得排序的搜索結果??梢允褂靡韵麓a:
function GetSortedSearchResults($options, $search_term) {
$sorted_results = array();
for ($count = 0; $count < count($options); $count++) {
$similarity = levenshtein($options["label"], $search_term);
$sorted_results[$similarity] = $options["label"];
}
ksort($sorted_results);
return array_values($sorted_results);
}

TA貢獻1712條經驗 獲得超3個贊
function searchForId ($id, $array) {foreach ($array as $key => $val) {if ($val['label'] === $id) {return $key;}}return null;} $ response[] = array("category"=>'cat_name',"label"=>'East Melbourne');$response[] = array("category"=>'cat_name',"label"=>'Melbourne' );$response[] = array("category"=>'cat_name',"label"=>'West Melbourne');$id = searchForId('Melbourne', $response);$newArr[]=$response[ $id];unset($response[$id]);$newArr1=array_merge($newArr,$response);echo "";print_r($newArr1);exit;
- 2 回答
- 0 關注
- 173 瀏覽
添加回答
舉報