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.