亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 PHP 中的搜索選項對多維數組進行排序?

如何使用 PHP 中的搜索選項對多維數組進行排序?

PHP
慕田峪7331174 2021-09-18 17:02:30
我創建了一個搜索框,在 keyup 事件中,我從其他文件中獲取 json_encoded 數組,但我面臨的問題是。例如。如果我輸入“墨爾本”,它會在完全匹配之前顯示“東墨爾本”和其他選項。我想先展示墨爾本,然后是東墨爾本、西墨爾本和其他選項。我想用確切的搜索字符串對其進行排序。The current array is like:$response[] = array("category"=>'cat_name',"label"=>'East Melbourne');$response[] = array("category"=>'cat_name',"label"=>'Melbourne');Output:{category: "City", label: "East Melbourne", value: "East Melbourne"}{category: "City", label: "Melbourne", value: "Melbourne"}
查看完整描述

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);

}


查看完整回答
反對 回復 2021-09-18
?
交互式愛情

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;


查看完整回答
反對 回復 2021-09-18
  • 2 回答
  • 0 關注
  • 173 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號