这是数组
$country_codes_with_euro_currency = array( 'AT', 'BE', 'CY', 'DE', 'EE', 'GR', 'ES', 'FI', 'FR', 'IE', 'IT', 'LU', 'MT', 'NL', 'PT', 'SI', 'SK' );
例如$result = 'at';
然后
if ( in_array(trim($result), $country_codes_with_euro_currency) ) {
echo $currency_code = 'EUR';
}
输出无关紧要。需要$result = 'AT';
所以想要检查UPPERCASE和小写,但不要手动以小写形式重写数组。
创建了这样的代码
$country_codes_with_euro_currency = array_merge( $country_codes_with_euro_currency, (array_map('strtolower', $country_codes_with_euro_currency)) );
有没有更好(更短)的解决方案?
...关于标记为重复只想告知我不要求如何将UPPERCASE转换为小写。在我的代码中,已经使用了strtolower
。我告诉我如何得到结果。并要求更好地了解如何获得相同的结果
最终解决方案
实际上对于这种情况,一个简单的解决方案。
保持$country_codes_with_euro_currency
不变(大写)。
简单地$result = strtoupper(trim($result));
。
然后if ( in_array(trim($result), $country_codes_with_euro_currency) )
,请问Does PHP include toupper and tolower functions?这样的答案(标记为重复)?我找不到......
答案 0 :(得分:2)
尝试使用strtoupper
和strtolower
if ( in_array(strtoupper(trim($result)), $country_codes_with_euro_currency)) {
echo $currency_code = 'EUR';
}
如果您想检查较小的案例,那么您可以将OR
置于条件
in_array(strtolower(trim($result)), $country_codes_with_euro_currency)
所以它应该像
if ( in_array(strtoupper(trim($result)), $country_codes_with_euro_currency) ||
in_array(strtolower(trim($result)), $country_codes_with_euro_currency)) {
echo $currency_code = 'EUR';
}
正如 JimL 所说,你可以在上面或下面更改结果和数组,如
$converted_array = array_map("strtoupper", $country_codes_with_euro_currency);
if ( in_array(strtoupper(trim($result)),$converted_array) )
{
echo $currency_code = 'EUR';
}