So I am trying to echo the value of a key if the key is present in the array. I've go this code at the moment:
<?php
$lingos = array(
"en"=>"en_US",
"en-gb"=>"en_GB",
"nl"=>"nl_NL",
"de"=>"de",
"da"=>"da",
"es"=>"es",
"ca"=>"es_MX",
"fr"=>"fr",
"it"=>"it",
"pt-pt"=>"pt_PT",
"no"=>"no",
"sv"=>"sv",
"fi"=>"fi",
"et"=>"en_GB",
"is"=>"en_GB",
"cs"=>"cs",
"pl"=>"pl",
"lv"=>"en_GB",
"lt"=>"lt",
"hu"=>"hu",
"ro"=>"ro",
"sr"=>"en_GB",
"hr"=>"en_GB",
"bg"=>"bg",
"el"=>"el",
"uk"=>"en_GB",
"ru"=>"ru",
"tr"=>"tr",
"ar"=>"ar",
"zh-hans"=>"zh_CN",
"zh-hant"=>"zh_TW",
"ja"=>"ja",
"ko"=>"ko",
"id"=>"in",
"ms"=>"ms",
"th"=>"th",
"vi"=>"vi",
"pt-br"=>"pt_PT"
);
// foreach($lingos as $lingo => $x_lingo) {
// echo "Key=" . $lingo . ", Value=" . $x_lingo;
// echo "<br>";
// }
$wmpl_langcode = ICL_LANGUAGE_CODE;
echo $wmpl_langcode;
if (array_key_exists($wmpl_langcode, $lingos)) {
echo $lingos[1];
} else {
echo "not found";
}
?>
Thing is, $lingos[1] is not returning anything. What am I doing wrong?
Presumably $wmpl_langcode
is something like en
as you are checking for it with array_key_exists
so use that as the index:
$wmpl_langcode = ICL_LANGUAGE_CODE;
echo $wmpl_langcode;
if (array_key_exists($wmpl_langcode, $lingos)) {
echo $lingos[$wmpl_langcode];
} else {
echo "not found";
}
Or simpler:
echo isset($lingos[$wmpl_langcode]) ? $lingos[$wmpl_langcode] : "not found";
//PHP 7+
echo $lingos[$wmpl_langcode] ?? "not found";