I have an array of all the countries in the world as such:

$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",);

And so on for all countries; I am 100% positive every country is listed properly.

I have an application form which stores the result in a file stored on the server. Currently the review page for the application is a basic text version and I am now in the process of putting it into a mock-form for my client to have a more visually appealing method of reviewing applications.

So an array named $in_data stores the results that come from the file. This array is structured as such "emergency_medical_insurance" => "value_user_entered". Each key is the name of the HTML element it came form and the value is what the user put in.

The country select list on the form returns a two-letter code of the country. So what I am trying to do is search $countries with the value of $in_data['country_select'] and then return the name of the country.

echo $in_data['country_select']; returns 'CA' the letter code for Canada and the test country I have entered.

echo $countries['CA']; returns 'Canada'

if (array_key_exists($in_data['country_select'], $countries)){ 
      echo "Country Found";
} 
else { echo "failed"; }

returns nothing.

if (array_key_exists('CA', $countries)){ 
      echo "Country Found";
} 
else { echo "failed"; }

Also returns nothing. And when I say nothing I mean nothing, not null, not true, not false; just doesn't even run.

My question is simple; how is the code below (taken from the offical PHP manual) which does EXACTLY the same thing my code does, working, but my code won't even return anything?

<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
    echo "The 'first' element is in the array";
}
?>
share|improve this question
1  
do var_dump($in_data['country_select']); instead so you can make sure the value is indeed a 2 char length string, it may have a leading/trailing space. – cryptic ツ Jan 22 '13 at 18:33
    
do var_dump($countries), echo $countries[$in_data['country_select']] and what @crypticツ suggest just before the if you have and tell us what they say please. Also, dummy thing but just to be sure, the code from the PHP manual works just fine in your site, right? – Francisco Presencia Jan 22 '13 at 18:33
    
your example works fine for me .. if array key exists CA countries, echoes Country Found – Adam MacDonald Jan 22 '13 at 18:35
    
Are you using PHP 4.0.7 or higher? You may also use isset() in this situation. – Eric Jan 22 '13 at 18:36
up vote 6 down vote accepted

since you are reading from a file, you may be getting other characters, try trim():

if (array_key_exists(trim($in_data['country_select']), $countries)){ 
      echo "Country Found";
} 
else { echo "failed"; }
share|improve this answer
    
This fixed it, thanks Adam; somehow a space was getting in there before the letters. – SC1988 Jan 22 '13 at 18:48
    
yw .. when reading lines from a file .. you may be getting CA\n instead of just CA – Adam MacDonald Jan 22 '13 at 18:51
    
After looking over the code I did to create the files themselves it appears that this is exactly what happened. – SC1988 Jan 22 '13 at 18:54

I had a similar problem while reading from a file. The solution was to remove the BOM from the first line.

function remove_utf8_bom($text) {
    $bom = pack('H*','EFBBBF');
    $text = preg_replace("/$bom/", '', $text);
    return $text;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.