Home » PHP » PHP Examples

PHP program to convert string to lowercase without using the library function

Sorting integer array in PHP: Here, we are going to learn how to sort an integer array in ascending or descending order in PHP?
Submitted by Bhanu Sharma, on August 08, 2019

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

Methods to sort an array

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

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

PHP code for sorting array in ascending order

<?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
//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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.