note 51775 deleted from reserved.variables by nlopess

From: Date: Mon, 11 Apr 2005 14:14:48 +0000
Subject: note 51775 deleted from reserved.variables by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-87836@lists.php.net to get a copy of this message
Note Submitter: javalizard at mac dot com ---- My web host server will give my php the user preferred languages out over the order. This means that I had to write a function for ordering the languages based upon their "q" value (rank from 1..0, 1 being the most preferred). If you want an ordered list of user preferred languages use this function: <?php function orderedLanguages() { $languages = split(",", $_SERVER['HTTP_ACCEPT_LANGUAGE'] ); $lang_q = Array(); foreach( $languages as $aLang ) { $lang_array = split(";q=", trim( $aLang ) ); $lang = trim( $lang_array[0] ); if( !isset( $lang_array[1] ) ) $q = 1; else $q = trim($lang_array[1]); $lang_q["$lang"] = (float)$q; } arsort($lang_q); //extra code for making the languages key indexed $i = 0; $lang_index = Array(); foreach($lang_q as $lang => $q) { // $lang_q[$i] = $lang; //add to the same array the index key/language $lang_index[$i] = $lang; //add to a new array the index key/language $i++; } //return $lang_index; // uncomment for returning array with keys={0..n-1}, values={most..least preferred} return $lang_q; } ?> While you can't reference the key by number, You can use foreach to pull elements. This will be in order. So getting the key with array_keys should work in the preferred order too. I've added a few extra lines of commented code for reordering the array into one(s) that reference the language by number (if you need it) :D

« previous php.notes (#87836) next »