我有我的頁面,在這裏用戶可以能夠搜索到美國各州找物業上的搜索功能。但是,在我的數據庫記錄中,僅在每個州名上設置了縮寫。所以我以陣列列表格式下載美國各州的副本。PHP in_array匹配搜索值的數組列表
碼圖1
$searchStates = array(
'Alabama' =>'AL',
'Alaska' =>'AK',
'Arizona' =>'AZ',
'Arkansas' =>'AR',
'California' =>'CA',
'Colorado' =>'CO',
'Connecticut' =>'CT',
'Delaware' =>'DE',
'Florida' =>'FL',
'Georgia' =>'GA',
'Hawaii' =>'HI',
'Idaho' =>'ID',
'Illinois' =>'IL',
'Indiana' =>'IN',
'Iowa' =>'IA',
'Kansas' =>'KS',
'Kentucky' =>'KY',
'Louisiana' =>'LA',
'Maine' =>'ME',
'Maryland' =>'MD',
'Massachusetts' =>'MA',
'Michigan' =>'MI',
'Minnesota' =>'MN',
'Mississippi' =>'MS',
'Missouri' =>'MO',
'Montana' =>'MT',
'Nebraska' =>'NE',
'Nevada' =>'NV',
'New Hampshire' =>'NH',
'New Jersey' =>'NJ',
'New Mexico' =>'NM',
'New York' =>'NY',
'North Carolina' =>'NC',
'North Dakota' =>'ND',
'Ohio' =>'OH',
'Oklahoma' =>'OK',
'Oregon' =>'OR',
'Pennsylvania' =>'PA',
'Rhode Island' =>'RI',
'South Carolina' =>'SC',
'South Dakota' =>'SD',
'Tennessee' =>'TN',
'Texas' =>'TX',
'Utah' =>'UT',
'Vermont' =>'VT',
'Virginia' =>'VA',
'Washington' =>'WA',
'West Virginia' =>'WV',
'Wisconsin' =>'WI',
'Wyoming' =>'WY'
);
案例:
我的客戶使用搜索州名,ex: Tennessee
但在我的結果,它會顯示由於縮寫無記錄只被保存在我的數據庫中。我想用這個數組列表$searchStates
來匹配我的縮寫狀態列表。我試圖使用in_array
函數,但它似乎不能正常工作。看到我的實際代碼。
碼圖2
if (in_array($search, $searchStates)) {
$property->where(function($query) use ($search) {
$query->where('property.state','like','%'.$search.'%')
->orWhere('property.contact_name','like','%'.$search.'%')
->orWhere('property.address','like','%'.$search.'%')
->orWhere('property.city','like','%'.$search.'%')
->orWhere('property.name','like','%'.$search.'%');
});
}
目標
我想用$searchStates
列表以配合我的數據庫記錄(這是在美國各州的縮寫形式),並顯示我的搜索結果頁面中的正確輸出。
問題
了這in_array
功能會工作或我應該使用其他功能?我會感謝你的幫助。在查詢
if (isset($searchStates[$search])) {
$search = $searchStates[$search];
// rest of your code
分享完整'碼圖。2'? – C2486
'if(isset($ searchStates [$ search])){ $ search = $ searchStates [$ search];' – splash58
你不能更新你的數據庫表的全名或者甚至創建一個新表並加入縮寫 – RamRaider