PHP,排序,sort_flags [英] PHP, sort, sort_flags
问题描述
我正在此页面上研究 sort_flags
.在PHP手册上.
I am studying sort_flags
at this page on PHP Manual.
我不明白这些标志分别代表什么区别.
And I don't understand what difference each of these flags represents.
只有6个标志,有人可以帮助我了解它们之间的区别吗?也许有一些例子.我会非常感激.
There are only 6 flags, can someone please help me to understand the difference between them. Maybe with some examples. I would be very thankful.
推荐答案
用于测试的数组:
$toSort = array(2, 1, "img1", "img2", "img10", 1.5, "3.14", "2.72");
请注意, 3.14&2.72
是字符串.
使用 SORT_REGULAR
标志(通常比较项目):
Using SORT_REGULAR
flag (compare items normally):
Array
(
[0] => 2.72
[1] => 3.14
[2] => img1
[3] => img10
[4] => img2
[5] => 1
[6] => 1.5
[7] => 2
)
使用 SORT_NUMERIC
标志(以数字方式比较项目,因此 3.14
以数字而不是字符串进行排序):
Using SORT_NUMERIC
flag (compare items numerically, so 3.14
is sorted as number not a string):
Array
(
[0] => img10
[1] => img1
[2] => img2
[3] => 1
[4] => 1.5
[5] => 2
[6] => 2.72
[7] => 3.14
)
使用 SORT_STRING
标志( SORT_LOCALE_STRING
的工作原理类似,但取决于当前的语言环境,所有值都视为字符串):
Using SORT_STRING
flag (SORT_LOCALE_STRING
works similary, but depends on current locale, all values are treated as strings):
Array
(
[0] => 1
[1] => 1.5
[2] => 2
[3] => 2.72
[4] => 3.14
[5] => img1
[6] => img10
[7] => img2
)
使用 SORT_NATURAL
标志(注意 img *
字符串的顺序,它是自然的):
Using SORT_NATURAL
flag (note order of img*
strings, it is natural):
Array
(
[0] => 1
[1] => 1.5
[2] => 2
[3] => 2.72
[4] => 3.14
[5] => img1
[6] => img2
[7] => img10
)
SORT_FLAG_CASE
可以与 SORT_STRING
或 SORT_NATURAL
组合以进行不区分大小写的排序,例如:
SORT_FLAG_CASE
can be combined with SORT_STRING
or SORT_NATURAL
to do case-insensitive sort e.g.:
// works like SORT_NATURAL but is case-insensitive
sort($toSort, SORT_NATURAL | SORT_FLAG_CASE);
这篇关于PHP,排序,sort_flags的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!