PHP array_key_exists doesn't work; array is not multidimensional

I have an array from 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% sure that each country is listed correctly.

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

So, the array with the name $in_data

stores the results obtained from the file. This array is structured as such "emergency_medical_insurance" => "value_user_entered"

. Each key is the name of the HTML element in which it appeared and the value is what the user entered.

In the country select list, the form returns the two-letter country code. So what I am trying to do is search $countries

with a value $in_data['country_select']

and then return the country name.

echo $in_data['country_select'];

returns the "CA" letter code for Canada and the test country I 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 won't start.

My question is simple; what does the code below look like (taken from the official PHP manual) that does EXACTLY the same as my code, works but my code won't 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";
}
?>

      

+3


source to share


2 answers


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"; }

      

+6


source


I had a similar problem when 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;
}

      

+1


source







All Articles