I have an associative array with data (let's say language codes and descriptions) and a second array with allowed keys (lang codes). I want to filter the data array by these allowed keys.
The problem is I'm bound to PHP 5.5 and I can't use ARRAY_FILTER_USE_KEY
flag.
I came up with the following solution:
$langs = [
'en' => "English",
'de' => "German",
'fr' => "French",
'ru' => "Russian",
];
$allowed_langs = ['en','de'];
var_export(
array_map( function($lang) use($langs) {return $langs[$lang];} , array_combine($allowed_langs, $allowed_langs))
);
/* Output as expected:
array (
'en' => 'English',
'de' => 'German',
)
*/
I wonder is there a more elegant and shorter solution to this task?