Obtaining Array Keys with a Given Value
<?php
$countries = array( 'USA' => 'English', 'Spain' => 'Spanish',
'France' => 'French', 'Argentina' => 'Spanish');
function array_get_keys($search, $array)
{
$keys = array();
foreach($array as $key => $value)
if($value == $search)
$keys[] = $key;
if(count($keys) == 0)
$keys = FALSE;
return $keys;
}
$language = 'Spanish';
$spoken = array_get_keys($language, $countries);
printf("<p>Countries where %s is spoken: %s.</p>\n",
$language,
$spoken ? implode(', ', $spoken) : 'None');
$language = 'Tagalog';
$spoken = array_get_keys($language, $countries);
printf("<p>Countries where %s is spoken: %s.</p>\n",
$language,
$spoken ? implode(', ', $spoken) : 'None');
?>
Related examples in the same category