Just on the topic of speed. One simple solution is to swap your key/value structure, so you have the city name as the key.
Then the lookup is very very fast.
Or, you can even do the below, which has both city=>code and code=>city
So, a lookup of either a cityname or code will return the alternate title. But, then this makes the array twice as big.
But, thats the trade off. Memory vs speed has always been an optimization issue.
<?php
$locality = array(
'AZ' => array(
'phoenix'=> 'Phoenix', 'Phoenix'=>'phoenix'
),
'CA' => array (
'la' => 'Los Angeles', 'Los Angeles' => 'la',
'sf' => 'San Francisco', 'San Francisco' => 'sf',
'fresno' => 'Fresno', 'Fresno' => 'fresno'
)
);
$city = 'Los Angeles';
foreach ($locality as $state => $value) if ($altname = $value[$city])
echo "state=$state altname=$altname name=$city";
?>
The other idea, is a binary search, but then you need to store the data differently again, since a binary search algorithm requires index keys.
Food for thought