PHP in_array 匹配搜索值到数组列表
PHP in_array match search value to array list
我的页面上有一个搜索功能,用户可以在其中搜索美国各州以找到 属性。但是,在我的数据库记录中,每个州名称都只设置了缩写。所以我以数组列表格式下载了美国各州的副本。
代码图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
函数是否有效,还是我应该使用其他函数?我会感谢你的帮助。
在查询中查找usyng的州名缩写形式
if (isset($searchStates[$search])) {
$search = $searchStates[$search];
// rest of your code
像这样使用 array_key_exists
:
$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'
);
if (array_key_exists( $search, $searchStates )) {
$searchTerm = $searchStates[$search];
$property->where(function($query) use ($searchTerm) {
$query->where('property.state','like','%'.$searchTerm.'%')
->orWhere('property.contact_name','like','%'.$searchTerm.'%')
->orWhere('property.address','like','%'.$searchTerm.'%')
->orWhere('property.city','like','%'.$searchTerm.'%')
->orWhere('property.name','like','%'.$searchTerm.'%');
});
}
好的,我最终在我的搜索功能中删除了其他 "queries"。因此,我意识到输入不仅会根据我需要搜索的内容匹配关键字,而且还会找到最接近的字符并匹配我在 属性 结果中的匹配项。这就是我的代码的最终结果。
if (array_key_exists( $search, $searchStates )) {
$searchTerm = $searchStates[$search];
$property->where(function($query) use ($searchTerm) {
$query->where('property.state','like','%'.$searchTerm.'%');
});
}
我设法选择了这个,而不是第一个,因为每当我在输入字段中输入全名时,都会恢复为缩写。
"Ex. If I type California, after triggered the search, the California will turn to CA inside the search input."
我的页面上有一个搜索功能,用户可以在其中搜索美国各州以找到 属性。但是,在我的数据库记录中,每个州名称都只设置了缩写。所以我以数组列表格式下载了美国各州的副本。
代码图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
函数是否有效,还是我应该使用其他函数?我会感谢你的帮助。
在查询中查找usyng的州名缩写形式
if (isset($searchStates[$search])) {
$search = $searchStates[$search];
// rest of your code
像这样使用 array_key_exists
:
$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'
);
if (array_key_exists( $search, $searchStates )) {
$searchTerm = $searchStates[$search];
$property->where(function($query) use ($searchTerm) {
$query->where('property.state','like','%'.$searchTerm.'%')
->orWhere('property.contact_name','like','%'.$searchTerm.'%')
->orWhere('property.address','like','%'.$searchTerm.'%')
->orWhere('property.city','like','%'.$searchTerm.'%')
->orWhere('property.name','like','%'.$searchTerm.'%');
});
}
好的,我最终在我的搜索功能中删除了其他 "queries"。因此,我意识到输入不仅会根据我需要搜索的内容匹配关键字,而且还会找到最接近的字符并匹配我在 属性 结果中的匹配项。这就是我的代码的最终结果。
if (array_key_exists( $search, $searchStates )) {
$searchTerm = $searchStates[$search];
$property->where(function($query) use ($searchTerm) {
$query->where('property.state','like','%'.$searchTerm.'%');
});
}
我设法选择了这个,而不是第一个,因为每当我在输入字段中输入全名时,都会恢复为缩写。
"Ex. If I type California, after triggered the search, the California will turn to CA inside the search input."