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

时间:2013-09-04 17:26:54

标签: 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";

我想按字母顺序排序。目前我手动执行此操作,但这很乏味有没有更简单的方法来执行此操作?

亲切的问候, 艾哈迈尔

6 个答案:

答案 0 :(得分:7)

我正在粘贴你正在做的事情:

  1. 拼接字符串
  2. 删除每个元素上的空格
  3. 删除空元素
  4. 排序数组
  5. 打印结果
  6. 我希望它对你有用

    <?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 :(得分:5)

试试这个:

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

答案 2 :(得分:2)

使用explode

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

答案 3 :(得分:2)

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

答案 4 :(得分:0)

我的答案部分基于@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);

答案 5 :(得分:0)

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