belongs to collection: PHP Array Programs
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,
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"; } ?>
100 89 88 80 22 21 19 11 10 10
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
PHP code for sorting array in ascending order
Output
PHP code for sorting array in descending order
Output
need an explanation for this answer? contact us directly to get an explanation for this answer