好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php数组函数排序之rsort() - 对数组的元素值进行降

php数组函数排序之rsort() - 对数组的元素值进行降序排序

本文章简单的讲解一下关于php利用自身的函数对数组的元素值进行降序排序方法,有需要的参考下.

rsort() 函数对数组的元素按照键值进行逆向排序.与 arsort() 的功能基本相同.

注释;该函数为 array 中的单元赋予新的键名.这将删除原有的键名而不仅是重新排序.

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

可选的第二个参数包含另外的排序标志.

语法

rsort(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() 函数.

实例代码如下:

<?php  $my_array  =  array ( "a"  =>  "Dog" ,  "b"  =>  "Cat" ,  "c"  =>  "Horse" );  rsort( $my_array );  print_r( $my_array );  ?>输出;  Array  (  [0] => Horse  [1] => Dog  [2] => Cat  )  Like sort(), rsort() assigns  new  keys  for  the elements in  array . It will remove any existing keys you may have assigned, rather than just reordering the keys.  This means that it will destroy associative keys.  $animals  =  array ( "dog" => "large" ,   "cat" => "medium" ,   "mouse" => "small" );  print_r( $animals );  //Array ( [dog] => large [cat] => medium [mouse] => small )   rsort( $animals );  print_r( $animals );  //Array ( [0] => small [1] => medium [2] => large )   Use KSORT()  or  KRSORT() to preserve associative keys. 

查看更多关于php数组函数排序之rsort() - 对数组的元素值进行降的详细内容...

  阅读:35次