Home  >  Article  >  Backend Development  >  php数组函数序列之sort() 对数组的元素值进行升序排序_php技巧

php数组函数序列之sort() 对数组的元素值进行升序排序_php技巧

WBOY
WBOYOriginal
2016-05-17 09:14:561166browse
sort()定义和用法
sort() 函数按升序对给定数组的值排序。

注释:本函数为数组中的单元赋予新的键名。原有的键名将被删除。

如果成功则返回 TRUE,否则返回 FALSE。

语法
sort(array,sorttype)参数 描述
array 必需。输入的数组。
sorttype 可选。规定如何排列数组的值。可能的值:

SORT_REGULAR - 默认。以它们原来的类型进行处理(不改变类型)。
SORT_NUMERIC - 把值作为数字来处理
SORT_STRING - 把值作为字符串来处理
SORT_LOCALE_STRING - 把值作为字符串来处理,基于本地设置*。


*:该值是 PHP 4.4.0 和 5.0.2 新加的。在 PHP 6 之前,使用了系统的区域设置,可以用 setlocale() 来改变。自 PHP 6 起,必须用 i18n_loc_set_default() 函数。

例子
复制代码 代码如下:

$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");

sort($my_array);
print_r($my_array);
?>

输出:

Array
(
0] => Cat
[1] => Dog
[2] => Horse
)
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn