note 51775 added to reserved.variables

From: Date: Sun, 10 Apr 2005 23:03:03 +0000
Subject: note 51775 added to reserved.variables
Groups: php.notes 
Request: Send a blank email to php-notes+get-87802@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/reserved.variables.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+51775 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51775&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51775&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51775&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51775&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51775&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+51775&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#87802) next »