I recently tried to create an array with multiple Strings, representing the AcceptLanguage header. I need to push another user-specified language to the start of the array, to make it max priority.
So far I have
function getRequestLangs(){
//get languages from browser
$accLangs = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
$requestedLanguages = array();
foreach($accLangs as $key => $lang){
$lang = substr($lang,0,2);
// p is a quality param, we won't need it, since the
// preferred languages are already sorted by default
if($lang != 'p='){
array_push($requestedLanguages,$lang);
}
}
// we only need each language once, this function keeps the order
return array_unique($requestedLanguages);
}
Now I want to add the user-specified language as first key (in case the language is not supportet, I may fall back to another accepted language)
//language from path, pushed as first index (highest priority)
if(isset($_GET['lang']) && $_GET['lang'] != ""){
$requestedLanguages = array_unshift($requestedLanguages,$_GET['lang']);
}
var_dump($requestedLanguages) before array_unshift:
array(2) {
[0]=> string(2) "de"
[2]=> string(2) "en"
}
var_dump($requestedLanguages) after array_unshift:
int(3)
I think it might have to do with the index-hole between 0 and 2, but that is only a guess.
[EDIT] i need more caffeine...
//wrong:
$requestedLanguages = array_unshift($requestedLanguages,$_GET['lang']);
//right:
array_unshift($requestedLanguages,$_GET['lang']);