百科狗-知识改变命运!
--

sort() - 对数组排序 - php 数组函数

乐乐1年前 (2023-11-21)阅读数 13#技术干货
文章标签数组

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() - 对数组排序 - php 数组函数

排序类型标记:

  • SORT_REGULAR-正常比较单元(不改变类型)
  • SORT_NUMERIC-单元被作为数字来比较
  • SORT_STRING-单元被作为字符串来比较
  • SORT_LOCALE_STRING-根据当前的区域(locale)设置来把单元当作字符串比较,可以用setlocale()来改变。
  • SORT_NATURAL-和natsort()类似对每个单元以“自然的顺序”对字符串进行排序。 PHP 5.4.0 中新增的。
  • SORT_FLAG_CASE-能够与SORT_STRINGSORT_NATURAL合并(OR 位运算),不区分大小写排序字符串。

返回值

成功时返回TRUE,或者在失败时返回FALSE

更新日志

版本说明
5.4.0添加了$sort_flags内SORT_NATURALSORT_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

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)