- array_map: documentation ( source)
<?php
$dict = [
'title' => [
'en' => 'Welcome',
'de' => 'Willkommen'
],
'description' => [
'en' => 'Hello',
'de' => 'Hallo'
]
];
function lang($phrase){
global $l, $dict; //Globals are bad, I know
$terms = array_map(function($item) use ($l) {
return $item[$l];
}, $dict);
return $terms[$phrase];
}
// English:
$l = 'de';
echo lang('title') . ' ' . lang('description') . '<br/>';
// German:
$l = 'en';
echo lang('title') . ' ' . lang('description');