antonyfal Posted July 14, 2011 Share Posted July 14, 2011 Hi i have a drop-down list this little piece is where i explode the comma deliminated words into an array.. everything works but i cant find a function to do the sort order of the value options? and also i get a blank space everytime.. as option and value? <optgroup label=\"Others added\">"; $profile_languagegeneralrep = str_replace(', ', ',',$profile_languagegeneral); $langwording = explode(',', $profile_languagegeneralrep); $langwording = array_unique($langwording); foreach($langwording as $langexi) $languageLists .= "<option value=\"".$langexi.','."\">".$langexi."</option>"; $languageLists .= "</optgroup>"; } $languageLists.="</select>"; $output['LANGUAGELISTS']=$languageLists; Quote Link to comment https://forums.phpfreaks.com/topic/242023-how-can-i-sort-this-alphabetically-and-remove-spaces-select-list-query/ Share on other sites More sharing options...
requinix Posted July 14, 2011 Share Posted July 14, 2011 How about sort? ""; $langwording = array_unique(array_map("trim", explode(",", $profile_languagegeneral))); sort($langwording); foreach ($langwording as $langexi) {... Quote Link to comment https://forums.phpfreaks.com/topic/242023-how-can-i-sort-this-alphabetically-and-remove-spaces-select-list-query/#findComment-1242894 Share on other sites More sharing options...
antonyfal Posted July 15, 2011 Author Share Posted July 15, 2011 Hi, thanks for the reply.. I haven't had the chance to test this yet-- does the "trim" remove the blank space/s? i think the sort will work.. Best regards antony Quote Link to comment https://forums.phpfreaks.com/topic/242023-how-can-i-sort-this-alphabetically-and-remove-spaces-select-list-query/#findComment-1243059 Share on other sites More sharing options...
TeNDoLLA Posted July 15, 2011 Share Posted July 15, 2011 trim — Strip whitespace (or other characters) from the beginning and end of a string : http://fi2.php.net/manual/en/function.trim.php . PHP.net manual is your friend. Quote Link to comment https://forums.phpfreaks.com/topic/242023-how-can-i-sort-this-alphabetically-and-remove-spaces-select-list-query/#findComment-1243074 Share on other sites More sharing options...
DavidAM Posted July 15, 2011 Share Posted July 15, 2011 The "blank space" as option and value is most likely the result of having a comma at the beginning or end of your original string. The explode() function splits the string on ALL occurrences of the delimiter. To prevent this from happening, remove the leading and/or trailing comma. You can do this with trim(). Based on the code from requinix: "<optgroup label=\"Others added\">"; $langwording = array_unique(array_map("trim", explode(",", trim($profile_languagegeneral, ',')))); sort($langwording); foreach ($langwording as $langexi) {... Quote Link to comment https://forums.phpfreaks.com/topic/242023-how-can-i-sort-this-alphabetically-and-remove-spaces-select-list-query/#findComment-1243084 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.