Take the 2-minute tour ×

I have an array:

$countryList = array(
  "GB" => "United Kingdom",
  "US" => "United States",
  "AF" => "Afghanistan",
  "AL" => "Albania",
  "DZ" => "Algeria"

and it goes on...

When I get a country location it comes out as, for example, United States, but I need to match that (the value) with the key, in this case 'US'. How would I do this? All the examples I have seen had to do with using the key to get the value when I need to use the value to get the key. Optimally the output will be just 'US', rather than that in an array.

I have tried echo array_keys('United States', $countryList); but it wants the first parameter to be in an array and it only comes in as United States, and I rather not change that to an array if possible.

share|improve this question

4 Answers 4

up vote 5 down vote accepted

You can use array_search function:

$key = array_search("United States", $countryList); // $key = 'US';
share|improve this answer
wow i even tried that and it literally didnt work until just now. not sure what i was doing so wrong –  Alex Aug 9 '12 at 15:48
Well hard to say without code, but this does work now, so :) –  walkhard Aug 9 '12 at 15:49
i see the problem was i am defining the country using geolocation, and it ends up being something like define('COUNTRY', $user_details['countryName']);, then when i enter COUNTRY instead of United States it doesnt work. Any idea why? Might have something to do with "" or ''? –  Alex Aug 9 '12 at 15:55
Ah the problem was my country comes in all caps and my values are Belgium rather than what it comes in like BELGIUM. So i had to do a ucfirst(strtolower(COUNTRY)). Now it works! –  Alex Aug 9 '12 at 16:04

array_search will give you the key that maps to the provided value.

$countryCode = array_search('United States', $countryList);

If you need to do this many times you can use array_flip to swap the keys with the values. This will provide faster lookup with a one-time cost of flipping.

$countriesByName = array_flip($countryList);
$countryCode = $countriesByName['United States'];
share|improve this answer
$array = array(
    'fruit1' => 'apple',
    'fruit2' => 'orange',
    'fruit3' => 'grape',
    'fruit4' => 'apple',
    'fruit5' => 'apple');

// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
    if ($fruit_name == 'apple') {
        echo key($array).'<br />';
share|improve this answer
This works but is slower than using the built-in functions in the other answers. –  David Harkness Aug 9 '12 at 15:52

Try this: array_keys($countryList, "United States");

share|improve this answer
Notice: Array to string conversion in C:\xampp\htdocs\neou_cms\framework\helpers\prepared_arrays.php on line 245 Array –  Alex Aug 9 '12 at 15:47
@Alex - This returns an array of the keys that map to the given value--even if there's only one key. –  David Harkness Aug 9 '12 at 15:50
yea it was different when the error occurred. thanks for the clarification –  Alex Aug 9 '12 at 15:53

Your Answer


