tags:

views:

63

answers:

6

Hi,

I have this array:

$a = array('it' => 'italiano',
                 'fr' => 'francese',
                 'en' => 'inglese',
                 'es' => 'spagnolo',
                 'de' => 'deutsch',

               );

Is there any function that i give as arguments the array and a key (for example 'en') and it returns 'inglese'?

Regards

Javi

+3  A: 

This doesn't necessitate a function. This is just standard array access logic.

$a['en']

Or if $key was 'en', then

$a[$key]
BBonifield
+1  A: 

Why not simply do:

echo $a['en'];

This will give you inglese

Sarfraz
My bad, fixed now.
Sarfraz
I don't know why but I can't undo my -1 vote :( I must have done something wrong... sorry.
Chouchenos
+1  A: 
$languageName = 'Default';
$languageCode = 'en';
if (isset($a[$languageCode])) {
   $languageName = $a[$languageCode];
}
Mark Baker
A: 

What's wrong with using $a['en']? Which would give 'inglese' as well.

function get_value($array, $key){
  return (isset($array[$key]) ? $array[$key] : null;
}

echo get_value($array, 'en'); # => echoes inglese
Nils Riedemann
A: 
function language($array, $language){
    if(isset($array[$language]))
       return $array[$language];
    else 
       return false;
}

$language = language($a, 'en');

if($language != false)
  echo $language;
Chouchenos
A: 

There are a couple of ways you can do this

$a = array(
      'it' => 'italiano',
      'fr' => 'francese',
      'en' => 'inglese',
      'es' => 'spagnolo',
      'de' => 'deutsch',
     );

$lang = 'it';
echo getValueFromKey($a,$lang);

function getValueFromKey($array,$key) {
   return $array[$key];
}

or like this

echo $a['it'];
Phill Pafford