已知三个升序整数数组_PHP程序按升序和降序对整数数组进行排序

已知三个升序整数数组

Given an integer array and we have to sort them in ascending and descending order in PHP.

给定一个整数数组,我们必须在PHP中按升序和降序对其进行排序。

Methods to sort an array

数组排序方法

In PHP, there are two methods which are used to sort an array,

在PHP中,有两种用于对数组进行排序的方法,

  1. sort() – It is used to sort an array in ascending order.

    sort() –用于按升序对数组进行排序。

  2. rsort() – It is used to sort an array in descending order.

    rsort() –用于按降序对数组进行排序。

PHP code for sorting array in ascending order

用于按升序对数组排序PHP代码

<?php
//declaring & initializing array of integers
$array = array(10, 80, 100, 11, 22, 21, 19, 10, 88, 89);

//sorting array in ascending order
sort ($array);

//printing array elements after sorting              
foreach( $array as $num ){
   
    echo $num."\n";
}
?>

Output

输出量

10
10
11
19
21
22
80
88
89
100

PHP code for sorting array in descending order

PHP代码以降序对数组进行排序

<?php
//declaring & initializing array of integers
$array = array(10, 80, 100, 11, 22, 21, 19, 10, 88, 89);

//sorting array in descending order
rsort ($array);

//printing array elements after sorting              
foreach( $array as $num ){
   
    echo $num."\n";
}
?>

Output

输出量

100
89
88
80
22
21
19
11
10
10


翻译自: https://www.includehelp.com/php/sort-an-integer-array-in-ascending-and-descending-order.aspx

已知三个升序整数数组

    原文作者:cumt30111
    原文地址: https://blog.csdn.net/cumt30111/article/details/107767288
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞