本文介绍了为什么SORT_REGULAR在PHP中产生不一致的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究一个使PHP中的数组排序更容易的类,并且我一直在使用SORT_常量,但是该行为或 SORT_REGULAR
(默认排序类型)似乎有所不同,具体取决于您在数组中添加项目的顺序。而且,我无法发现为什么会这样。
I'm working on a class which makes sorting of arrays easier in PHP and I've been playing with the SORT_ constants, however the behaviour or SORT_REGULAR
(the default sort type) seems to differ depending what order you add the items in your array. Moreover, I can't spot a pattern as to why this might be the case.
数组项:
$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"
}
有什么想法吗?
推荐答案
排序时要小心数组具有混合类型值,因为sort()
会产生不可预测的结果。
Be careful when sorting arrays with mixed types values because sort() can produce unpredictable results.
您应该使用SORT_ *常量之一。
You should use one of the SORT_* constants.
这里有一些评论:
- Numeric sort an array with mixed types values
- PHP turtles
这篇关于为什么SORT_REGULAR在PHP中产生不一致的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!