PHP中基于语言环境的数组排序在OS X上的结果与Ubuntu的结果不同 [英] Locale based sorting of arrays in PHP gives different result on OS X than Ubuntu

查看:80
本文介绍了PHP中基于语言环境的数组排序在OS X上的结果与Ubuntu的结果不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下数组:

$foo = ["a", "B", "æ", "Æ", "c", "A", "b", "C", "1"];

然后我用setlocale(LC_ALL, ['nb_NO.UTF-8', 'no_NO.UTF-8']);设置语言环境,并通过sort($foo, SORT_LOCALE_STRING)运行数组.

Then I set the locale with setlocale(LC_ALL, ['nb_NO.UTF-8', 'no_NO.UTF-8']); and run the array through sort($foo, SORT_LOCALE_STRING).

在Ubuntu中,排序后的数组将如下所示:

In Ubuntu, the sorted array will then look like:

[
  0 => '1',
  1 => 'A',
  2 => 'a',
  3 => 'B',
  4 => 'b',
  5 => 'C',
  6 => 'c',
  7 => 'Æ',
  8 => 'æ',
]

在Mac(OS X)上,我得到:

While on Mac (OS X) I get:

[
  0 => '1',
  1 => 'A',
  2 => 'B',
  3 => 'C',
  4 => 'a',
  5 => 'b',
  6 => 'c',
  7 => 'Æ',
  8 => 'æ',
]

似乎OS X希望自己对以大写字母开头的字符串进行排序(ABC,然后是abc),而我希望它们在一起(AaBbCc).

It seems OS X wants to sort strings starting with capital letters by themselves(ABC, then abc), while I would just like them to be together (AaBbCc).

有什么方法可以让它们在PHP中以相同的方式对数组进行排序,还是我必须使用u*sort()方法之一来编写自定义排序方法?

Is there any way of having them sort the arrays the same way in PHP, or would I have to write a custom sorting method, using one of the u*sort() methods instead?

似乎与类似,该问题已被标记作为的副本.尽管OS X似乎仍然可以将大写和小写字母依次排序而不是混合使用,但可以通过在排序功能中添加strtolower()来解决.

seems to be quite similar to the question this is marked as a duplicate of. Altough OS X still seems to sort upper- and lower case letters after each other instead of mixing them, it is fixable by adding strtolower() to the sorting function.

推荐答案

解决此问题的最干净,最专业的方法是,确保您在两种环境中实际上设置了相同的语言环境.请通读手册和规范下方的注释,以获取更多见解.有关如何正确设置和获取语言环境的信息.设置语言环境可能很麻烦,您可能需要更改某些语言的语法,并声明其他语言的后备值.辛劳是您的责任.

The cleanest, most professional way to fix this issue would be to ensure that you are actually setting an identical locale in both environments. Please read though the manual and the comments presented below the specs for insights about how to properly set and get the locale. Setting the locale can be fiddly business, you may need to change the syntax for some, and declare fallback values for others. The toil is yours to undertake.

对于热/临时修复,您可以通过将飞船运算符置于一系列条件之间来调用usort().首先,在多字节安全地将$a$b都转换为大写之后,将它们进行比较,如果该比较是平局,则太空飞船操作员将对原始的$a$b进行比较.

As for a hot/temporary fix, you can call usort() with the spaceship operator placed between an array of conditions. First, compare $a vs $b after multibyte-safe converting them both to uppercase, if that comparison is a tie, then the spaceship operator will make a comparison on the raw $a vs $b.

代码:(演示)

$foo = ["a", "B", "æ", "Æ", "c", "A", "b", "C", "1"];

usort($foo, function($a, $b) {
    return [mb_strtoupper($a), $a] <=> [mb_strtoupper($b), $b];
});

var_export($foo);

输出:

array (
  0 => '1',
  1 => 'A',
  2 => 'a',
  3 => 'B',
  4 => 'b',
  5 => 'C',
  6 => 'c',
  7 => 'Æ',
  8 => 'æ',
)

这篇关于PHP中基于语言环境的数组排序在OS X上的结果与Ubuntu的结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆