PHP array_key_exists不起作用;数组不是多维的
作者:互联网
我有一系列世界上所有国家:
$countries = array(
"GB" => "United Kingdom",
"US" => "United States",
"AF" => "Afghanistan",
"AL" => "Albania",
"DZ" => "Algeria",
"AS" => "American Samoa",
"AD" => "Andorra",
"AO" => "Angola",
"AI" => "Anguilla",
"AQ" => "Antarctica",
"AG" => "Antigua And Barbuda",
"AR" => "Argentina",
"AM" => "Armenia",
"AW" => "Aruba",
"AU" => "Australia",
"AT" => "Austria",
"AZ" => "Azerbaijan",
"BS" => "Bahamas",
"BH" => "Bahrain",
"BD" => "Bangladesh",
"BB" => "Barbados",
"BY" => "Belarus",
"BE" => "Belgium",
"BZ" => "Belize",
"BJ" => "Benin",
"BM" => "Bermuda",
"BT" => "Bhutan",
"BO" => "Bolivia",
"BA" => "Bosnia And Herzegowina",
"BW" => "Botswana",
"BV" => "Bouvet Island",);
所有国家都是如此;我100%肯定每个国家都正确列出.
我有一个申请表,将结果存储在服务器上存储的文件中.目前,该应用程序的评论页面是一个基本的文本版本,我现在正在将它放入一个模拟表单中,以便我的客户有一个更具视觉吸引力的方法来审查应用程序.
因此,名为$in_data的数组存储来自文件的结果.该阵列的结构如下:“emergency_medical_insurance”=> “value_user_entered”.每个键都是它所形成的HTML元素的名称,值是用户输入的值.
表单上的国家/地区选择列表返回该国家/地区的双字母代码.所以我想要做的是搜索价值为$in_data [‘country_select’]的$country,然后返回该国家/地区的名称.
echo $in_data [‘country_select’];返回’CA’加拿大的字母代码和我输入的测试国家/地区.
echo $countries [‘CA’];返回’加拿大’
if (array_key_exists($in_data['country_select'], $countries)){
echo "Country Found";
}
else { echo "failed"; }
没有回报.
if (array_key_exists('CA', $countries)){
echo "Country Found";
}
else { echo "failed"; }
也没有任何回报.当我什么都不说时,我什么都不说,不是空的,不是真的,不是虚假的;只是甚至没有运行.
我的问题很简单;下面的代码(取自官方的PHP手册)如何与我的代码完全相同,工作,但我的代码甚至不返回任何东西?
<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}
?>
解决方法:
因为你正在读取文件,你可能会得到其他字符,尝试trim():
if (array_key_exists(trim($in_data['country_select']), $countries)){
echo "Country Found";
}
else { echo "failed"; }
标签:html,php,arrays,forms,array-key-exists 来源: https://codeday.me/bug/20190729/1569373.html