rsort和asort_PHP rsort()函数与示例

rsort和asort

PHP rsort()函数 (PHP rsort() function)

rsort() function is used to sort an indexed array in descending order.

rsort()函数用于按降序对索引数组进行排序。

If array elements are numeric, it sorts based on the numbers, if array elements are the string, it sorts based on the alphabets and if the array contains numeric values and text/strings, it sorts elements based on the alphabets.

如果数组元素是数字,则根据数字排序;如果数组元素是字符串,则根据字母排序;如果数组包含数字值和文本/字符串,则根据字母排序。

It does not return a sorted array, it sorts the input array.

它不返回已排序的数组,而是对输入数组进行排序。

Syntax:

句法:

    rsort(array, [mode]);

Here,

这里,

  • array is an input array

    数组是输入数组

  • mode is an optional parameter, its default value is 0, it has following values:

    mode是一个可选参数,其默认值为0,它具有以下值:

    0 – It is used to compare items normally

    0 –用于正常比较项目

    1 – It is used to compare items numerically

    1 –用于数字比较项目

    2 – It is used to compare items as strings

    2 –用于比较项目作为字符串

    3 – It is used to compare items as current locale strings

    3 –用于比较项目作为当前区域设置字符串

    4 – It is used to compare items as strings (natural ordering)

    4 –用于将项目作为字符串进行比较(自然顺序)

Examples:

例子:

    Input:
    $arr1 = array(105, 108, 101, 100, 90);

    Output:
    arr1 (sorted)...
    Array
    (    
        [0] => 108  
        [1] => 105  
        [2] => 101  
        [3] => 100  
        [4] => 90   
    )

PHP code:

PHP代码:

<?php    
    $arr1 = array(105, 108, 101, 100, 90);
    $arr2 = array("Amit", "Prem", "Prerana", "Aleesha", "Abhishek");
	
    rsort($arr1);
    print ("arr1 (sorted)...\n");
    print_r ($arr1);

    rsort($arr2);
    print ("arr2 (sorted)...\n");
    print_r ($arr2);    
?>

Output

输出量

arr1 (sorted)...
Array
(    
    [0] => 108  
    [1] => 105  
    [2] => 101  
    [3] => 100  
    [4] => 90   
)    
arr2 (sorted)...
Array
(    
    [0] => Prerana 
    [1] => Prem 
    [2] => Amit 
    [3] => Aleesha 
    [4] => Abhishek
)


翻译自: https://www.includehelp.com/php/rsort-function-with-example.aspx

rsort和asort

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值