以逗号分隔的列表按字母PHP排序


Sort comma separated list Alphabetically PHP

我将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";

我想按字母顺序排序。目前我手动做这件事,但很乏味。有什么更简单的方法吗?

谨致问候,Ahmar

我给你粘贴一个它正在做的事情的片段:

  1. 拼接管柱
  2. 删除每个元素上的空格
  3. 删除空元素
  4. 排序数组
  5. 打印结果

我希望它对你有用

<?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);

试试这个:

$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);
// 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);

使用explode:

$xyz_arr = explode(',', $xyz);
sort($xyz_arr);

我的答案部分基于@Carlos的答案,但在这里和那里进行了一些调整,并封装到一个可重用的函数中。它应该能够完成这项工作,对吧!

/* 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] */

要使用,只需调用这样的函数:

$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);
  1. 首先使用爆炸函数在逗号上爆炸字符串
  2. 使用array_map()函数删除空白
  3. 使用asort()函数按升序对数组进行排序
  4. 然后使用内爆()将数组转换为字符串


$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);