sort() - 对数组排序 - php 数组函数
sort()
(PHP 4, PHP 5, PHP 7)
对数组排序
说明
sort(array &$array[,int $sort_flags= SORT_REGULAR]): bool本函数对数组进行排序。当本函数结束时数组单元将被从最低到最高重新安排。
Note:If two members compare as equal, their relative order in the sorted array is undefined.
参数
$array要排序的数组。
$sort_flags可选的第二个参数$sort_flags可以用以下值改变排序的行为:
排序类型标记:
SORT_REGULAR
-正常比较单元(不改变类型)SORT_NUMERIC
-单元被作为数字来比较SORT_STRING
-单元被作为字符串来比较SORT_LOCALE_STRING
-根据当前的区域(locale)设置来把单元当作字符串比较,可以用setlocale()来改变。SORT_NATURAL
-和natsort()类似对每个单元以“自然的顺序”对字符串进行排序。 PHP 5.4.0 中新增的。SORT_FLAG_CASE
-能够与SORT_STRING
或SORT_NATURAL
合并(OR 位运算),不区分大小写排序字符串。
返回值
成功时返回TRUE
,或者在失败时返回FALSE
。
更新日志
版本 | 说明 |
---|---|
5.4.0 | 添加了$sort_flags内SORT_NATURAL 和SORT_FLAG_CASE 的支持。 |
5.0.2 | 添加了SORT_LOCALE_STRING 。 |
范例
Example #1sort()例子
以上例程会输出:
fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] = orange
fruits 被按照字母顺序排序。
使用不区分大小写自然排序的sort()例子
以上例程会输出:
fruits[0] = Orange1 fruits[1] = orange2 fruits[2] = Orange3 fruits[3] = orange20
fruits 排序得像natcasesort()的结果。
注释
Note:此函数为$array中的元素赋与新的键名。这将删除原有的键名,而不是仅仅将键名重新排序。
Note:和大多数 PHP 排序函数一样,sort()使用了» Quicksort实现的。 The pivot is chosen in the middle of the partition resulting in an optimal time for already sorted arrays. This is however an implementation detail you shouldn't rely on.
Warning在对含有混合类型值的数组排序时要小心,因为sort()可能会产生不可预知的结果。
参见
asort()
对数组进行排序并保持索引关系- 数组排序函数对比
Simple function to sort an array by a specific key. Maintains index association.
unless you specify the second argument, "regular" comparisons will be used. I quote from the page on comparison operators: "If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically." What this means is that "10" "2". In other words, regular PHP string comparisons are not transitive. This implies that the output of sort() can in rare cases depend on the order of the input array:
Sorting the keys, but keep the values in order is not possible by just ordering, because it would result in a new array. This is also the solution: Create a new array
Commenting on note http://www.php.net/manual/en/function.sort.php#62311 : Sorting an array of objects will not always yield the results you desire. As pointed out correctly in the note above, sort() sorts the array by value of the first member variable. However, you can not always assume the order of your member variables! You must take into account your class hierarchy! By default, PHP places the inherited member variables on top, meaning your first member variable is NOT the first variable in your class definition! However, if you use code analyzers or a compile cache, things can be very different. E.g., in eAccelerator, the inherited member variables are at the end, meaning you get different sort results with caching on or off. Conclusion: Never use sort on arrays with values of a type other than scalar or array.
This took me longer than it should have to figure out, but if you want the behavior of sort($array, SORT_STRING) (that is, re-indexing the array unlike natcasesort) in a case-insensitive manner, it is a simple matter of doing usort($array, strcasecmp).
This function will sort entity letters eg:é I hope that help someone function sort_entity($array) { $total = count($array); for ($i=0;$i Ex:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!