简体   繁体   English

无法按字母顺序排列数组

[英]Cannot sort array alphabetically

I need to sort an array alphabetically after that I add new items to it, the problem's that sort function is not working as expected:在向数组添加新项目之后,我需要按字母顺序对数组进行排序,问题是sort函数没有按预期工作:

$opts = "Air conditioning, Balcony, Bedding, Concrete flooring, Garage, Garden, Heating, Internet, Laundry, Outdoor Kitchen, Parking, Swimming Pool, TV Cable, Tennis courts, Trees,Gym,access to private beach,access to private beach club,air conditioning,air heating,alfresco dining,alfresco shower,american style coffee maker,barbecue,bath amenities,breakfast patio area,cooking area,dishwasher,doorman,dryer machine,dvd player,hairdryer,heated pool,hi fi system,large fridge,laundry area,living room,maid service,main terrace,microwave,modern kitchen,oven,panoramic terrace,pool,pool with jet stream,refrigerator,safe,sat tv,solarium,terrace,washing machine,wi fi";
$opts = explode(',', $opts);
$test = array('hello world');

$updated_opts = array_merge($test, $opts);
sort($updated_opts);
var_dump($updated_opts);

the output is the following:输出如下:

array(53) {
  [0]=>
  string(8) " Balcony"
  [1]=>
  string(8) " Bedding"
  [2]=>
  string(18) " Concrete flooring"
  [3]=>
  string(7) " Garage"
  [4]=>
  string(7) " Garden"
  [5]=>
  string(8) " Heating"
  [6]=>
  string(9) " Internet"
  [7]=>
  string(8) " Laundry"
  [8]=>
  string(16) " Outdoor Kitchen"
  [9]=>
  string(8) " Parking"
  [10]=>
  string(14) " Swimming Pool"
  [11]=>
  string(9) " TV Cable"
  [12]=>
  string(14) " Tennis courts"
  [13]=>
  string(6) " Trees"
  [14]=>
  string(16) "Air conditioning"
  [15]=>
  string(3) "Gym"
  [16]=>
  string(23) "access to private beach"
  [17]=>
  string(28) "access to private beach club"
  [18]=>
  string(16) "air conditioning"
  [19]=>
  string(11) "air heating"
  [20]=>
  string(15) "alfresco dining"
  [21]=>
  string(15) "alfresco shower"
  [22]=>
  string(27) "american style coffee maker"
  [23]=>
  string(8) "barbecue"
  [24]=>
  string(14) "bath amenities"
  [25]=>
  string(20) "breakfast patio area"
  [26]=>
  string(12) "cooking area"
  [27]=>
  string(10) "dishwasher"
  [28]=>
  string(7) "doorman"
  [29]=>
  string(13) "dryer machine"
  [30]=>
  string(10) "dvd player"
  [31]=>
  string(9) "hairdryer"
  [32]=>
  string(11) "heated pool"
  [33]=>
  string(11) "hello world"
  [34]=>
  string(12) "hi fi system"
  [35]=>
  string(12) "large fridge"
  [36]=>
  string(12) "laundry area"
  [37]=>
  string(11) "living room"
  [38]=>
  string(12) "maid service"
  [39]=>
  string(12) "main terrace"
  [40]=>
  string(9) "microwave"
  [41]=>
  string(14) "modern kitchen"
  [42]=>
  string(4) "oven"
  [43]=>
  string(17) "panoramic terrace"
  [44]=>
  string(4) "pool"
  [45]=>
  string(20) "pool with jet stream"
  [46]=>
  string(12) "refrigerator"
  [47]=>
  string(4) "safe"
  [48]=>
  string(6) "sat tv"
  [49]=>
  string(8) "solarium"
  [50]=>
  string(7) "terrace"
  [51]=>
  string(15) "washing machine"
  [52]=>
  string(5) "wi fi"
}

Apply trim to remove spaces in the beginning of the words:应用trim删除单词开头的空格:

$opts = "Air conditioning, Balcony, Bedding, Concrete flooring, Garage, Garden, Heating, Internet, Laundry, Outdoor Kitchen, Parking, Swimming Pool, TV Cable, Tennis courts, Trees,Gym,access to private beach,access to private beach club,air conditioning,air heating,alfresco dining,alfresco shower,american style coffee maker,barbecue,bath amenities,breakfast patio area,cooking area,dishwasher,doorman,dryer machine,dvd player,hairdryer,heated pool,hi fi system,large fridge,laundry area,living room,maid service,main terrace,microwave,modern kitchen,oven,panoramic terrace,pool,pool with jet stream,refrigerator,safe,sat tv,solarium,terrace,washing machine,wi fi";
$opts = explode(',', $opts);
// here
$opts = array_map('trim', $opts);
$test = array('hello world');

$updated_opts = array_merge($test, $opts);
sort($updated_opts);
// for case insensitive sort use second argument
// sort($updated_opts, SORT_STRING | SORT_FLAG_CASE);
var_dump($updated_opts);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM