strcoll() 的行为有时有点令人困惑。它取决于您区域设置中的 LC_COLLATE。
<?php
$a = 'a';
$b = 'A';
print strcmp ($a, $b) . "\n"; setlocale (LC_COLLATE, 'C');
print "C: " . strcoll ($a, $b) . "\n"; setlocale (LC_COLLATE, 'de_DE');
print "de_DE: " . strcoll ($a, $b) . "\n"; setlocale (LC_COLLATE, 'de_CH');
print "de_CH: " . strcoll ($a, $b) . "\n"; setlocale (LC_COLLATE, 'en_US');
print "en_US: " . strcoll ($a, $b) . "\n"; ?>
这在想要使用 strcoll 对数组进行排序时很有用
<?php
$a = array ('a', 'A', '?', '?', 'b', 'B');
setlocale (LC_COLLATE, 'C');
usort ($a, 'strcoll');
print_r ($a);
?>
这类似于 sort($a)
数组
(
[0] => A
[1] => B
[2] => a
[3] => b
[4] => ?
[5] => ?
)
<?php
setlocale (LC_COLLATE, 'de_DE');
usort ($a, 'strcoll');
print_r ($a)
?>
这完全不同
数组
(
[0] => a
[1] => A
[2] => ?
[3] => ?
[4] => b
[5] => B
)