3

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

asked Sep 4, 2013 at 17:26
3
  • 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. Commented Sep 4, 2013 at 17:28
  • and how you want the result back? Commented Sep 4, 2013 at 17:30
  • In ascending or descending order alphabetically Commented Sep 4, 2013 at 17:31

6 Answers 6

8

I am pasting you a snippet what it is doing:

  1. Splicing string
  2. Remove blank spaces on each element
  3. Remove empty elements
  4. Sorting array
  5. 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);
answered Sep 4, 2013 at 17:47
Sign up to request clarification or add additional context in comments.

1 Comment

asort doesn't just sort the array, it maintains key association. To just "sort the array" you might you sort($array).
5

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);
John Parker
54.5k11 gold badges133 silver badges131 bronze badges
answered Sep 4, 2013 at 17:30

1 Comment

explode will cause issues with the inconsistent spaces. Try preg_split('/,\s*/') instead.
3
// 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);
answered Sep 4, 2013 at 17:34

1 Comment

$array = array(); foreach($parts as $part) { array_push($array,$part); } is pointless.
2

use explode:

$xyz_arr = explode(',', $xyz);
sort($xyz_arr);
answered Sep 4, 2013 at 17:31

Comments

0

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);
answered Jan 3, 2020 at 11:40

Comments

0
  1. First explode the string on comma with explode function.
  2. Remove white space with array_map() function.
  3. Sort the array in ascending order with asort() function.
  4. 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);
answered Jan 3, 2020 at 12:39

2 Comments

Please explain step by step your solution so it is clear to everyone reading it.
i have added explanation please review.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.