I have the data in a PHP variable in a comma separated list. Here is the data (some part of it to save space)
$xyz="Europe, France, Italy, Spain, UK, US,Nordic, West Europe, Belgium, Luxembourg, Netherlands, Sweden,US,Asia, Europe, Israel, North America, India,North America, , China, Hong Kong,West North Central, West South Central,UK,East South Central,Middle Atlantic, Greater China, Malaysia, Singapore, Taiwan, Middle Atlantic, Global, Australasia, Central and East Europe";
I want to sort this alphabetically. Currently I do this manually but it is tedious Is there any easier way to do this?
Kind regards, Ahmar
-
1"easier way" than what? How are you currently attempting to do it? Perhaps there is a simple fix to what you've done so far.Wiseguy– Wiseguy2013年09月04日 17:28:56 +00:00Commented Sep 4, 2013 at 17:28
-
and how you want the result back?bansi– bansi2013年09月04日 17:30:48 +00:00Commented Sep 4, 2013 at 17:30
-
In ascending or descending order alphabeticallyAhmar Ali– Ahmar Ali2013年09月04日 17:31:09 +00:00Commented Sep 4, 2013 at 17:31
6 Answers 6
I am pasting you a snippet what it is doing:
- Splicing string
- Remove blank spaces on each element
- Remove empty elements
- Sorting array
- Printing the result
I hope it is usefull for you
<?php
$xyz = "Europe, France, Italy, Spain, UK, US,Nordic, West Europe, Belgium, Luxembourg, Netherlands, Sweden,US,Asia, Europe, Israel, North America, India,North America, , China, Hong Kong,West North Central, West South Central,UK,East South Central,Middle Atlantic, Greater China, Malaysia, Singapore, Taiwan, Middle Atlantic, Global, Australasia, Central and East Europe";
$array = array_filter(array_map('trim', explode(',', $xyz)));
asort($array);
$array = implode(', ', $array);
print_r($array);
1 Comment
asort doesn't just sort the array, it maintains key association. To just "sort the array" you might you sort($array).Try this:
$xyz="Europe, France, Italy, Spain, UK, US,Nordic, West Europe, Belgium, Luxembourg, Netherlands, Sweden,US,Asia, Europe, Israel, North America, India,North America, , China, Hong Kong,West North Central, West South Central,UK,East South Central,Middle Atlantic, Greater China, Malaysia, Singapore, Taiwan, Middle Atlantic, Global, Australasia, Central and East Europe";
$arr = explode(',', $xyz);
asort($arr);
print_r($arr);
1 Comment
explode will cause issues with the inconsistent spaces. Try preg_split('/,\s*/') instead.// break apart the string at each comma
$parts = explode(',',$xyz);
// create an array
$array = array();
// loop through $parts and put each country into the new array
foreach($parts as $part) {
array_push($array,$part);
}
// sort the array alphabetically
asort($array);
print_r($array);
1 Comment
$array = array(); foreach($parts as $part) { array_push($array,$part); } is pointless.use explode:
$xyz_arr = explode(',', $xyz);
sort($xyz_arr);
Comments
My answer is partially based off of @Carlos answer but with a few tweaks here-and-there, and wrapped into a reusable function. It should be more than capable of getting the job done, right!
/* A global function to sort a [STRING] comma-delimited list both alphabetically, and numerically, and then return the sorted [STRING] list back to the function caller. [BEGIN] */
if (!function_exists('sortCommaDelimitedListStr'))
{
function sortCommaDelimitedListStr($var)
{
$arr = array_filter(array_map('trim', explode(',', $var)));
sort($arr);
$arr = implode(', ', $arr);
return $arr;
};
};
/* A global function to sort a [STRING] comma-delimited list both alphabetically, and numerically, and then return the sorted [STRING] list back to the function caller. [END] */
To use, simply call the function like so:
$xyz = "Europe, France, Italy, Spain, UK, US,Nordic, West Europe, Belgium, Luxembourg, Netherlands, Sweden,US,Asia, Europe, Israel, North America, India,North America, , China, Hong Kong,West North Central, West South Central,UK,East South Central,Middle Atlantic, Greater China, Malaysia, Singapore, Taiwan, Middle Atlantic, Global, Australasia, Central and East Europe";
$xyz = sortCommaDelimitedListStr($xyz);
Comments
- First explode the string on comma with explode function.
- Remove white space with array_map() function.
- Sort the array in ascending order with asort() function.
- Then convert array into string with implode().
$xyz="Europe, France, Italy, Spain, UK, US,Nordic, West Europe, Belgium, Luxembourg, Netherlands, Sweden,US,Asia, Europe, Israel, North America, India,North America, , China, Hong Kong,West North Central, West South Central,UK,East South Central,Middle Atlantic, Greater China, Malaysia, Singapore, Taiwan, Middle Atlantic, Global, Australasia, Central and East Europe";
$xyz = explode(',',$xyz);
$trimSpaces = array_map('trim',$xyz);
asort($trimSpaces);
$xyz = implode(',',$trimSpaces);
print_r($xyz);