2

我在逗号分隔列表中的 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";

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

亲切的问候,艾哈迈尔

4

6 回答 6

7

我正在向您粘贴一个片段,它在做什么:

  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);
于 2013-09-04T17:47:49.017 回答
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);
于 2013-09-04T17:30:01.837 回答
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);
于 2013-09-04T17:34:31.753 回答
2

使用explode

$xyz_arr = explode(',', $xyz);
sort($xyz_arr);
于 2013-09-04T17:31:28.623 回答
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);
于 2020-01-03T11:40:53.857 回答
0
  1. 首先使用explode 函数来分解逗号上的字符串。
  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);
于 2020-01-03T12:39:29.923 回答