我正在研究一个使 PHP 中的数组排序更容易的类,并且我一直在使用 SORT_ 常量,但是行为或 SORT_REGULAR
(默认排序类型)似乎有所不同取决于您在数组中添加项目的顺序。此外,我无法找出为什么会出现这种情况的模式。
数组项:
$a = '0.3';
$b = '.5';
$c = '4';
$d = 'F';
$e = 'z';
$f = 4;
场景 1:
sort(array($d, $e, $a, $f, $b, $c));
// Produces...
array(6) {
[0]=>
string(3) "0.3"
[1]=>
string(2) ".5"
[2]=>
string(1) "4"
[3]=>
string(1) "F"
[4]=>
string(1) "z"
[5]=>
int(4)
}
场景 2:
sort(array($d, $e, $b, $f, $c, $a));
// Produces...
array(6) {
[0]=>
string(3) "0.3"
[1]=>
string(2) ".5"
[2]=>
string(1) "F"
[3]=>
string(1) "z"
[4]=>
int(4)
[5]=>
string(1) "4"
}
有什么想法吗?
最佳答案
Warning
Be careful when sorting arrays with mixed types values because sort() can produce unpredictable results.
您应该使用 SORT_* 常量之一。
这里有几点评论:
关于php - 为什么 SORT_REGULAR 在 PHP 中产生不一致的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20792028/