Try this:
function get_sector($sector_array, $state_name) {
foreach ($sector_array as $sec)
if (in_array($state_name, $sec))
return $sec['name'];
return false;
}
Given $state
and $sectors
as defined in your question...
echo get_sector($sectors, $state); // prints "Southwest"
UPDATE regarding comment:
There are several different approaches to the "not found" return value. What works best for your application is up to you, but here are a few examples:
return false;
//This would be used like this:
if (!$sect = get_sector($sectors, $state))
echo "A custom error message"
else
echo "Sector = " . $sect;
OR you could return a specific string:
return "Sector not found.";
// Return a standard error message
OR a customized string:
return "No sector match for the state of " . $state_name;
// Return a specific error message