什么更有效率?连接到数据库或在数组中有几百行?

What is more efficient? Connecting to the DB or having a couple hundred rows in an array?

我正在尝试获取访问者的呼叫代码和国家/地区名称。

以前,我使用

连接到数据库以获取与我们从 CloudFlare 获得的相匹配的调用代码和国家/地区名称
$_SERVER["HTTP_CF_IPCOUNTRY"];

$country_code=$_SERVER["HTTP_CF_IPCOUNTRY"];
$stmt = $pdo->query('SELECT short_name, calling_code from country WHERE iso2 LIKE "'.$country_code.'"');
$country = $stmt->fetch();

但是,当流量变大时,与数据库的许多连接可能会出现问题。所以我定义了这个数组:

$countries = array();
$countries['AF'] = array("code"=>"AF","short_name"=>"Afghanistan","calling_code"=>"93");
$countries['AL'] = array("code"=>"AL","short_name"=>"Albania","calling_code"=>"355");
$countries['DZ'] = array("code"=>"DZ","short_name"=>"Algeria","calling_code"=>"213");
$countries['AS'] = array("code"=>"AS","short_name"=>"American Samoa","calling_code"=>"1");
$countries['AD'] = array("code"=>"AD","short_name"=>"Andorra","calling_code"=>"376");
$countries['AO'] = array("code"=>"AO","short_name"=>"Angola","calling_code"=>"244");
$countries['AI'] = array("code"=>"AI","short_name"=>"Anguilla","calling_code"=>"1");
$countries['AG'] = array("code"=>"AG","short_name"=>"Antigua","calling_code"=>"1");
$countries['AR'] = array("code"=>"AR","short_name"=>"Argentina","calling_code"=>"54");
$countries['AM'] = array("code"=>"AM","short_name"=>"Armenia","calling_code"=>"374");
$countries['AW'] = array("code"=>"AW","short_name"=>"Aruba","calling_code"=>"297");
$countries['AU'] = array("code"=>"AU","short_name"=>"Australia","calling_code"=>"61");
$countries['AT'] = array("code"=>"AT","short_name"=>"Austria","calling_code"=>"43");
$countries['AZ'] = array("code"=>"AZ","short_name"=>"Azerbaijan","calling_code"=>"994");
$countries['BH'] = array("code"=>"BH","short_name"=>"Bahrain","calling_code"=>"973");
$countries['BD'] = array("code"=>"BD","short_name"=>"Bangladesh","calling_code"=>"880");
$countries['BB'] = array("code"=>"BB","short_name"=>"Barbados","calling_code"=>"1");
$countries['BY'] = array("code"=>"BY","short_name"=>"Belarus","calling_code"=>"375");
$countries['BE'] = array("code"=>"BE","short_name"=>"Belgium","calling_code"=>"32");
$countries['BZ'] = array("code"=>"BZ","short_name"=>"Belize","calling_code"=>"501");
$countries['BJ'] = array("code"=>"BJ","short_name"=>"Benin","calling_code"=>"229");
$countries['BM'] = array("code"=>"BM","short_name"=>"Bermuda","calling_code"=>"1");
$countries['BT'] = array("code"=>"BT","short_name"=>"Bhutan","calling_code"=>"975");
$countries['BO'] = array("code"=>"BO","short_name"=>"Bolivia","calling_code"=>"591");
$countries['BA'] = array("code"=>"BA","short_name"=>"Bosnia and Herzegovina","calling_code"=>"387");
$countries['BW'] = array("code"=>"BW","short_name"=>"Botswana","calling_code"=>"267");
$countries['BR'] = array("code"=>"BR","short_name"=>"Brazil","calling_code"=>"55");
$countries['IO'] = array("code"=>"IO","short_name"=>"British Indian Ocean Territory","calling_code"=>"246");
$countries['VG'] = array("code"=>"VG","short_name"=>"British Virgin Islands","calling_code"=>"1");
$countries['BN'] = array("code"=>"BN","short_name"=>"Brunei","calling_code"=>"673");
$countries['BG'] = array("code"=>"BG","short_name"=>"Bulgaria","calling_code"=>"359");
$countries['BF'] = array("code"=>"BF","short_name"=>"Burkina Faso","calling_code"=>"226");
$countries['MM'] = array("code"=>"MM","short_name"=>"Burma Myanmar" ,"calling_code"=>"95");
$countries['BI'] = array("code"=>"BI","short_name"=>"Burundi","calling_code"=>"257");
$countries['KH'] = array("code"=>"KH","short_name"=>"Cambodia","calling_code"=>"855");
$countries['CM'] = array("code"=>"CM","short_name"=>"Cameroon","calling_code"=>"237");
$countries['CA'] = array("code"=>"CA","short_name"=>"Canada","calling_code"=>"1");
$countries['CV'] = array("code"=>"CV","short_name"=>"Cape Verde","calling_code"=>"238");
$countries['KY'] = array("code"=>"KY","short_name"=>"Cayman Islands","calling_code"=>"1");
$countries['CF'] = array("code"=>"CF","short_name"=>"Central African Republic","calling_code"=>"236");
$countries['TD'] = array("code"=>"TD","short_name"=>"Chad","calling_code"=>"235");
$countries['CL'] = array("code"=>"CL","short_name"=>"Chile","calling_code"=>"56");
$countries['CN'] = array("code"=>"CN","short_name"=>"China","calling_code"=>"86");
$countries['CO'] = array("code"=>"CO","short_name"=>"Colombia","calling_code"=>"57");
$countries['KM'] = array("code"=>"KM","short_name"=>"Comoros","calling_code"=>"269");
$countries['CK'] = array("code"=>"CK","short_name"=>"Cook Islands","calling_code"=>"682");
$countries['CR'] = array("code"=>"CR","short_name"=>"Costa Rica","calling_code"=>"506");
$countries['CI'] = array("code"=>"CI","short_name"=>"Côte d'Ivoire" ,"calling_code"=>"225");
$countries['HR'] = array("code"=>"HR","short_name"=>"Croatia","calling_code"=>"385");
$countries['CU'] = array("code"=>"CU","short_name"=>"Cuba","calling_code"=>"53");
$countries['CY'] = array("code"=>"CY","short_name"=>"Cyprus","calling_code"=>"357");
$countries['CZ'] = array("code"=>"CZ","short_name"=>"Czech Republic","calling_code"=>"420");
$countries['CD'] = array("code"=>"CD","short_name"=>"Democratic Republic of Congo","calling_code"=>"243");
$countries['DK'] = array("code"=>"DK","short_name"=>"Denmark","calling_code"=>"45");
$countries['DJ'] = array("code"=>"DJ","short_name"=>"Djibouti","calling_code"=>"253");
$countries['DM'] = array("code"=>"DM","short_name"=>"Dominica","calling_code"=>"1");
$countries['DO'] = array("code"=>"DO","short_name"=>"Dominican Republic","calling_code"=>"1");
$countries['EC'] = array("code"=>"EC","short_name"=>"Ecuador","calling_code"=>"593");
$countries['EG'] = array("code"=>"EG","short_name"=>"Egypt","calling_code"=>"20");
$countries['SV'] = array("code"=>"SV","short_name"=>"El Salvador","calling_code"=>"503");
$countries['GQ'] = array("code"=>"GQ","short_name"=>"Equatorial Guinea","calling_code"=>"240");
$countries['ER'] = array("code"=>"ER","short_name"=>"Eritrea","calling_code"=>"291");
$countries['EE'] = array("code"=>"EE","short_name"=>"Estonia","calling_code"=>"372");
$countries['ET'] = array("code"=>"ET","short_name"=>"Ethiopia","calling_code"=>"251");
$countries['FK'] = array("code"=>"FK","short_name"=>"Falkland Islands","calling_code"=>"500");
$countries['FO'] = array("code"=>"FO","short_name"=>"Faroe Islands","calling_code"=>"298");
$countries['FM'] = array("code"=>"FM","short_name"=>"Federated States of Micronesia","calling_code"=>"691");
$countries['FJ'] = array("code"=>"FJ","short_name"=>"Fiji","calling_code"=>"679");
$countries['FI'] = array("code"=>"FI","short_name"=>"Finland","calling_code"=>"358");
$countries['FR'] = array("code"=>"FR","short_name"=>"France","calling_code"=>"33");
$countries['GF'] = array("code"=>"GF","short_name"=>"French Guiana","calling_code"=>"594");
$countries['PF'] = array("code"=>"PF","short_name"=>"French Polynesia","calling_code"=>"689");
$countries['GA'] = array("code"=>"GA","short_name"=>"Gabon","calling_code"=>"241");
$countries['GE'] = array("code"=>"GE","short_name"=>"Georgia","calling_code"=>"995");
$countries['DE'] = array("code"=>"DE","short_name"=>"Germany","calling_code"=>"49");
$countries['GH'] = array("code"=>"GH","short_name"=>"Ghana","calling_code"=>"233");
$countries['GI'] = array("code"=>"GI","short_name"=>"Gibraltar","calling_code"=>"350");
$countries['GR'] = array("code"=>"GR","short_name"=>"Greece","calling_code"=>"30");
$countries['GL'] = array("code"=>"GL","short_name"=>"Greenland","calling_code"=>"299");
$countries['GD'] = array("code"=>"GD","short_name"=>"Grenada","calling_code"=>"1");
$countries['GP'] = array("code"=>"GP","short_name"=>"Guadeloupe","calling_code"=>"590");
$countries['GU'] = array("code"=>"GU","short_name"=>"Guam","calling_code"=>"1");
$countries['GT'] = array("code"=>"GT","short_name"=>"Guatemala","calling_code"=>"502");
$countries['GN'] = array("code"=>"GN","short_name"=>"Guinea","calling_code"=>"224");
$countries['GW'] = array("code"=>"GW","short_name"=>"Guinea-Bissau","calling_code"=>"245");
$countries['GY'] = array("code"=>"GY","short_name"=>"Guyana","calling_code"=>"592");
$countries['HT'] = array("code"=>"HT","short_name"=>"Haiti","calling_code"=>"509");
$countries['HN'] = array("code"=>"HN","short_name"=>"Honduras","calling_code"=>"504");
$countries['HK'] = array("code"=>"HK","short_name"=>"Hong Kong","calling_code"=>"852");
$countries['HU'] = array("code"=>"HU","short_name"=>"Hungary","calling_code"=>"36");
$countries['IS'] = array("code"=>"IS","short_name"=>"Iceland","calling_code"=>"354");
$countries['IN'] = array("code"=>"IN","short_name"=>"India","calling_code"=>"91");
$countries['ID'] = array("code"=>"ID","short_name"=>"Indonesia","calling_code"=>"62");
$countries['IR'] = array("code"=>"IR","short_name"=>"Iran","calling_code"=>"98");
$countries['IQ'] = array("code"=>"IQ","short_name"=>"Iraq","calling_code"=>"964");
$countries['IE'] = array("code"=>"IE","short_name"=>"Ireland","calling_code"=>"353");
$countries['IL'] = array("code"=>"IL","short_name"=>"Israel","calling_code"=>"972");
$countries['IT'] = array("code"=>"IT","short_name"=>"Italy","calling_code"=>"39");
$countries['JM'] = array("code"=>"JM","short_name"=>"Jamaica","calling_code"=>"1");
$countries['JP'] = array("code"=>"JP","short_name"=>"Japan","calling_code"=>"81");
$countries['JO'] = array("code"=>"JO","short_name"=>"Jordan","calling_code"=>"962");
$countries['KZ'] = array("code"=>"KZ","short_name"=>"Kazakhstan","calling_code"=>"7");
$countries['KE'] = array("code"=>"KE","short_name"=>"Kenya","calling_code"=>"254");
$countries['KI'] = array("code"=>"KI","short_name"=>"Kiribati","calling_code"=>"686");
$countries['XK'] = array("code"=>"XK","short_name"=>"Kosovo","calling_code"=>"381");
$countries['KW'] = array("code"=>"KW","short_name"=>"Kuwait","calling_code"=>"965");
$countries['KG'] = array("code"=>"KG","short_name"=>"Kyrgyzstan","calling_code"=>"996");
$countries['LA'] = array("code"=>"LA","short_name"=>"Laos","calling_code"=>"856");
$countries['LV'] = array("code"=>"LV","short_name"=>"Latvia","calling_code"=>"371");
$countries['LB'] = array("code"=>"LB","short_name"=>"Lebanon","calling_code"=>"961");
$countries['LS'] = array("code"=>"LS","short_name"=>"Lesotho","calling_code"=>"266");
$countries['LR'] = array("code"=>"LR","short_name"=>"Liberia","calling_code"=>"231");
$countries['LY'] = array("code"=>"LY","short_name"=>"Libya","calling_code"=>"218");
$countries['LI'] = array("code"=>"LI","short_name"=>"Liechtenstein","calling_code"=>"423");
$countries['LT'] = array("code"=>"LT","short_name"=>"Lithuania","calling_code"=>"370");
$countries['LU'] = array("code"=>"LU","short_name"=>"Luxembourg","calling_code"=>"352");
$countries['MO'] = array("code"=>"MO","short_name"=>"Macau","calling_code"=>"853");
$countries['MK'] = array("code"=>"MK","short_name"=>"Macedonia","calling_code"=>"389");
$countries['MG'] = array("code"=>"MG","short_name"=>"Madagascar","calling_code"=>"261");
$countries['MW'] = array("code"=>"MW","short_name"=>"Malawi","calling_code"=>"265");
$countries['MY'] = array("code"=>"MY","short_name"=>"Malaysia","calling_code"=>"60");
$countries['MV'] = array("code"=>"MV","short_name"=>"Maldives","calling_code"=>"960");
$countries['ML'] = array("code"=>"ML","short_name"=>"Mali","calling_code"=>"223");
$countries['MT'] = array("code"=>"MT","short_name"=>"Malta","calling_code"=>"356");
$countries['MH'] = array("code"=>"MH","short_name"=>"Marshall Islands","calling_code"=>"692");
$countries['MQ'] = array("code"=>"MQ","short_name"=>"Martinique","calling_code"=>"596");
$countries['MR'] = array("code"=>"MR","short_name"=>"Mauritania","calling_code"=>"222");
$countries['MU'] = array("code"=>"MU","short_name"=>"Mauritius","calling_code"=>"230");
$countries['YT'] = array("code"=>"YT","short_name"=>"Mayotte","calling_code"=>"262");
$countries['MX'] = array("code"=>"MX","short_name"=>"Mexico","calling_code"=>"52");
$countries['MD'] = array("code"=>"MD","short_name"=>"Moldova","calling_code"=>"373");
$countries['MC'] = array("code"=>"MC","short_name"=>"Monaco","calling_code"=>"377");
$countries['MN'] = array("code"=>"MN","short_name"=>"Mongolia","calling_code"=>"976");
$countries['ME'] = array("code"=>"ME","short_name"=>"Montenegro","calling_code"=>"382");
$countries['MS'] = array("code"=>"MS","short_name"=>"Montserrat","calling_code"=>"1");
$countries['MA'] = array("code"=>"MA","short_name"=>"Morocco","calling_code"=>"212");
$countries['MZ'] = array("code"=>"MZ","short_name"=>"Mozambique","calling_code"=>"258");
$countries['NA'] = array("code"=>"NA","short_name"=>"Namibia","calling_code"=>"264");
$countries['NR'] = array("code"=>"NR","short_name"=>"Nauru","calling_code"=>"674");
$countries['NP'] = array("code"=>"NP","short_name"=>"Nepal","calling_code"=>"977");
$countries['NL'] = array("code"=>"NL","short_name"=>"Netherlands","calling_code"=>"31");
$countries['AN'] = array("code"=>"AN","short_name"=>"Netherlands Antilles","calling_code"=>"599");
$countries['NC'] = array("code"=>"NC","short_name"=>"New Caledonia","calling_code"=>"687");
$countries['NZ'] = array("code"=>"NZ","short_name"=>"New Zealand","calling_code"=>"64");
$countries['NI'] = array("code"=>"NI","short_name"=>"Nicaragua","calling_code"=>"505");
$countries['NE'] = array("code"=>"NE","short_name"=>"Niger","calling_code"=>"227");
$countries['NG'] = array("code"=>"NG","short_name"=>"Nigeria","calling_code"=>"234");
$countries['NU'] = array("code"=>"NU","short_name"=>"Niue","calling_code"=>"683");
$countries['NF'] = array("code"=>"NF","short_name"=>"Norfolk Island","calling_code"=>"672");
$countries['KP'] = array("code"=>"KP","short_name"=>"North Korea","calling_code"=>"850");
$countries['MP'] = array("code"=>"MP","short_name"=>"Northern Mariana Islands","calling_code"=>"1");
$countries['NO'] = array("code"=>"NO","short_name"=>"Norway","calling_code"=>"47");
$countries['OM'] = array("code"=>"OM","short_name"=>"Oman","calling_code"=>"968");
$countries['PK'] = array("code"=>"PK","short_name"=>"Pakistan","calling_code"=>"92");
$countries['PW'] = array("code"=>"PW","short_name"=>"Palau","calling_code"=>"680");
$countries['PS'] = array("code"=>"PS","short_name"=>"Palestine","calling_code"=>"970");
$countries['PA'] = array("code"=>"PA","short_name"=>"Panama","calling_code"=>"507");
$countries['PG'] = array("code"=>"PG","short_name"=>"Papua New Guinea","calling_code"=>"675");
$countries['PY'] = array("code"=>"PY","short_name"=>"Paraguay","calling_code"=>"595");
$countries['PE'] = array("code"=>"PE","short_name"=>"Peru","calling_code"=>"51");
$countries['PH'] = array("code"=>"PH","short_name"=>"Philippines","calling_code"=>"63");
$countries['PL'] = array("code"=>"PL","short_name"=>"Poland","calling_code"=>"48");
$countries['PT'] = array("code"=>"PT","short_name"=>"Portugal","calling_code"=>"351");
$countries['PR'] = array("code"=>"PR","short_name"=>"Puerto Rico","calling_code"=>"1");
$countries['QA'] = array("code"=>"QA","short_name"=>"Qatar","calling_code"=>"974");
$countries['CG'] = array("code"=>"CG","short_name"=>"Republic of the Congo","calling_code"=>"242");
$countries['RE'] = array("code"=>"RE","short_name"=>"Réunion" ,"calling_code"=>"262");
$countries['RO'] = array("code"=>"RO","short_name"=>"Romania","calling_code"=>"40");
$countries['RU'] = array("code"=>"RU","short_name"=>"Russia","calling_code"=>"7");
$countries['RW'] = array("code"=>"RW","short_name"=>"Rwanda","calling_code"=>"250");
$countries['BL'] = array("code"=>"BL","short_name"=>"Saint Barthélemy" ,"calling_code"=>"590");
$countries['SH'] = array("code"=>"SH","short_name"=>"Saint Helena","calling_code"=>"290");
$countries['KN'] = array("code"=>"KN","short_name"=>"Saint Kitts and Nevis","calling_code"=>"1");
$countries['MF'] = array("code"=>"MF","short_name"=>"Saint Martin","calling_code"=>"590");
$countries['PM'] = array("code"=>"PM","short_name"=>"Saint Pierre and Miquelon","calling_code"=>"508");
$countries['VC'] = array("code"=>"VC","short_name"=>"Saint Vincent and the Grenadines","calling_code"=>"1");
$countries['WS'] = array("code"=>"WS","short_name"=>"Samoa","calling_code"=>"685");
$countries['SM'] = array("code"=>"SM","short_name"=>"San Marino","calling_code"=>"378");
$countries['ST'] = array("code"=>"ST","short_name"=>"São Tomé and Príncipe" ,"calling_code"=>"239");
$countries['SA'] = array("code"=>"SA","short_name"=>"Saudi Arabia","calling_code"=>"966");
$countries['SN'] = array("code"=>"SN","short_name"=>"Senegal","calling_code"=>"221");
$countries['RS'] = array("code"=>"RS","short_name"=>"Serbia","calling_code"=>"381");
$countries['SC'] = array("code"=>"SC","short_name"=>"Seychelles","calling_code"=>"248");
$countries['SL'] = array("code"=>"SL","short_name"=>"Sierra Leone","calling_code"=>"232");
$countries['SG'] = array("code"=>"SG","short_name"=>"Singapore","calling_code"=>"65");
$countries['SK'] = array("code"=>"SK","short_name"=>"Slovakia","calling_code"=>"421");
$countries['SI'] = array("code"=>"SI","short_name"=>"Slovenia","calling_code"=>"386");
$countries['SB'] = array("code"=>"SB","short_name"=>"Solomon Islands","calling_code"=>"677");
$countries['SO'] = array("code"=>"SO","short_name"=>"Somalia","calling_code"=>"252");
$countries['ZA'] = array("code"=>"ZA","short_name"=>"South Africa","calling_code"=>"27");
$countries['KR'] = array("code"=>"KR","short_name"=>"South Korea","calling_code"=>"82");
$countries['ES'] = array("code"=>"ES","short_name"=>"Spain","calling_code"=>"34");
$countries['LK'] = array("code"=>"LK","short_name"=>"Sri Lanka","calling_code"=>"94");
$countries['LC'] = array("code"=>"LC","short_name"=>"St. Lucia","calling_code"=>"1");
$countries['SD'] = array("code"=>"SD","short_name"=>"Sudan","calling_code"=>"249");
$countries['SR'] = array("code"=>"SR","short_name"=>"Surishort_name","calling_code"=>"597");
$countries['SZ'] = array("code"=>"SZ","short_name"=>"Swaziland","calling_code"=>"268");
$countries['SE'] = array("code"=>"SE","short_name"=>"Sweden","calling_code"=>"46");
$countries['CH'] = array("code"=>"CH","short_name"=>"Switzerland","calling_code"=>"41");
$countries['SY'] = array("code"=>"SY","short_name"=>"Syria","calling_code"=>"963");
$countries['TW'] = array("code"=>"TW","short_name"=>"Taiwan","calling_code"=>"886");
$countries['TJ'] = array("code"=>"TJ","short_name"=>"Tajikistan","calling_code"=>"992");
$countries['TZ'] = array("code"=>"TZ","short_name"=>"Tanzania","calling_code"=>"255");
$countries['TH'] = array("code"=>"TH","short_name"=>"Thailand","calling_code"=>"66");
$countries['BS'] = array("code"=>"BS","short_name"=>"The Bahamas","calling_code"=>"1");
$countries['GM'] = array("code"=>"GM","short_name"=>"The Gambia","calling_code"=>"220");
$countries['TL'] = array("code"=>"TL","short_name"=>"Timor-Leste","calling_code"=>"670");
$countries['TG'] = array("code"=>"TG","short_name"=>"Togo","calling_code"=>"228");
$countries['TK'] = array("code"=>"TK","short_name"=>"Tokelau","calling_code"=>"690");
$countries['TO'] = array("code"=>"TO","short_name"=>"Tonga","calling_code"=>"676");
$countries['TT'] = array("code"=>"TT","short_name"=>"Trinidad and Tobago","calling_code"=>"1");
$countries['TN'] = array("code"=>"TN","short_name"=>"Tunisia","calling_code"=>"216");
$countries['TR'] = array("code"=>"TR","short_name"=>"Turkey","calling_code"=>"90");
$countries['TM'] = array("code"=>"TM","short_name"=>"Turkmenistan","calling_code"=>"993");
$countries['TC'] = array("code"=>"TC","short_name"=>"Turks and Caicos Islands","calling_code"=>"1");
$countries['TV'] = array("code"=>"TV","short_name"=>"Tuvalu","calling_code"=>"688");
$countries['UG'] = array("code"=>"UG","short_name"=>"Uganda","calling_code"=>"256");
$countries['UA'] = array("code"=>"UA","short_name"=>"Ukraine","calling_code"=>"380");
$countries['AE'] = array("code"=>"AE","short_name"=>"United Arab Emirates","calling_code"=>"971");
$countries['GB'] = array("code"=>"GB","short_name"=>"United Kingdom","calling_code"=>"44");
$countries['US'] = array("code"=>"US","short_name"=>"United States","calling_code"=>"1");
$countries['UY'] = array("code"=>"UY","short_name"=>"Uruguay","calling_code"=>"598");
$countries['VI'] = array("code"=>"VI","short_name"=>"US Virgin Islands","calling_code"=>"1");
$countries['UZ'] = array("code"=>"UZ","short_name"=>"Uzbekistan","calling_code"=>"998");
$countries['VU'] = array("code"=>"VU","short_name"=>"Vanuatu","calling_code"=>"678");
$countries['VA'] = array("code"=>"VA","short_name"=>"Vatican City","calling_code"=>"39");
$countries['VE'] = array("code"=>"VE","short_name"=>"Venezuela","calling_code"=>"58");
$countries['VN'] = array("code"=>"VN","short_name"=>"Vietnam","calling_code"=>"84");
$countries['WF'] = array("code"=>"WF","short_name"=>"Wallis and Futuna","calling_code"=>"681");
$countries['YE'] = array("code"=>"YE","short_name"=>"Yemen","calling_code"=>"967");
$countries['ZM'] = array("code"=>"ZM","short_name"=>"Zambia","calling_code"=>"260");
$countries['ZW'] = array("code"=>"ZW","short_name"=>"Zimbabwe","calling_code"=>"263");

我只是获取我想要使用的值:

$country_code=$_SERVER["HTTP_CF_IPCOUNTRY"];
$country=$countries[''.$country_code.''];

因此无需连接到数据库并使用 MYSQL。

我的问题是,这个数组每秒加载几次是不是很糟糕?会不会对性能影响很大?我应该像以前那样简单地连接到数据库吗?

服务器有 16GB 内存和 8 个内核。我们说的是每天 4 万到 5 万次访问。

谢谢!

首先:$country=$countries[''.$country_code.''];可以换成这个$country=$countries[$country_code];

这个数组绝对是比数据库更好的选择。每个连接都需要一些时间,并且可以更快地访问数组。

如果您想确切知道差异有多大,可以使用此 "mini test suite" 确定运行时差异。

$start1 = microtime(true);

for($i = 0; $i < 10000; $i++)
{
    //With database
}
$end1 = microtime(true);

unset(); // The variables which might have been set the section above

$start2 = microtime(true);
for($i = 0; $i < 10000; $i++)
{
    //code with array
}
$end2 = microtime(true);

echo $end1-$start1."<br />";
echo $end2-$start2."<br />";

希望对你有帮助,如果没有请告诉我。