8

If I have this array

$england = array(
        'AVN' => 'Avon',
        'BDF' => 'Bedfordshire',
        'BRK' => 'Berkshire',
        'BKM' => 'Buckinghamshire',
        'CAM' => 'Cambridgeshire',
        'CHS' => 'Cheshire'
);

I want to be able to get the three letter code from the full text version, how would i write the following function:

$text_input = 'Cambridgeshire';
function get_area_code($text_input){
    //cross reference array here
    //fish out the KEY, in this case 'CAM'
    return $area_code;
}

thanks!

2 Answers 2

25

Use array_search():

$key = array_search($value, $array);

So, in your code:

// returns the key or false if the value hasn't been found.
function get_area_code($text_input) {
    global $england;
    return array_search($england, $text_input);
}

If you want it case insensitive, you can use this function instead of array_search():

function array_isearch($haystack, $needle) {
   foreach($haystack as $key => $val) {
       if(strcasecmp($val, $needle) === 0) {
           return $key;
       }
   }
   return false;
}

If you array values are regular expressions, you can use this function:

function array_pcresearch($haystack, $needle) {
   foreach($haystack as $key => $val) {
       if(preg_match($val, $needle)) {
           return $key;
       }
   }
   return false;
}

In this case you have to ensure all values in your array are valid regular expressions.

However, if the values are coming from an <input type="select">, there's a better solution: Instead of <option>Cheshire</option> use <option value="CHS">Cheshire</option>. Then the form will submit the specified value instead of the displayed name and you won't have to do any searching in your array; you'll just have to check for isset($england[$text_input]) to ensure a valid code has been sent.

Sign up to request clarification or add additional context in comments.

7 Comments

+1 @Haroldo be aware that array_search is case sensitive when working with strings.
Is it possible to do array_seach, where the value is a regular expression? Then i could use preg_quote and account for abbreviations and different versions of and...
@Thief - excellent advice on using the select input, thanks!
Importing the haystack with global is a bad idea. It should be passed to the function as parameter. Then the function would be just an alias of array_search and you should use array_search directly.
@Haroldo: Added a regex example to my answer. @rik: Indeed. Global variables are bad, especially for stuff like that.
|
6

If all values in $england are unique, you can do:

$search = array_flip($england);
$area_code = $search['Cambridgeshire'];

1 Comment

That code creates a new array which is certainly less efficient than iteratinv over an existing one. However, it is better than array_search etc. you have to lookup many values as array key->value lookups most likely use a hash map while value->key lookups require iteration.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.