-2

I have this array in PHP:

Array
(
    [en-CA] => English
    [fr-CA] => Français
    [es-ES] => Español
)

I would like to get the language name from the key.

So I made this but it doesn't work:

$lang = "en-CA";
$curLang = array_search($lang, $languages);

$curLang returns me nothing.

Thanks.

F__M
  • 1,518
  • 1
  • 19
  • 34

3 Answers3

0

array_search — Searches the array for a given value and returns the first corresponding key if successful

You are actually searching for a key, not a value.

So you can just do this to get the value you want.

$curLang = $languages[$lang];
BizzyBob
  • 12,309
  • 4
  • 27
  • 51
0

Try this:

$languages =   Array
   (
     [en-CA] => English
     [fr-CA] => Français
     [es-ES] => Español
  )

 $lang = "en-CA";
 $curLang = $languages[$lang];
mith
  • 1,680
  • 1
  • 10
  • 12
0

You can searching for a key, not a value.

<?php
$languages = array(

    'en-CA' => 'English',
    'fr-CA' => 'Français',
    'es-ES' => 'Español',
);  

$lang = "en-CA";
if (array_key_exists($lang, $languages)) {
  echo $languages[$lang];
}
?>
Arun
  • 1,609
  • 1
  • 15
  • 18