Array sorting

PHP - array sort function

In this chapter, we will introduce the following PHP array sorting functions one by one:

     Sort() - sorts the array in ascending order

     Rsort() - sorts the array in descending order

     Asort() - ascending the array according to the value of the associative array

     Ksort() - ascending the array according to the keys of the associative array

     Arsort() - sorts the array in descending order according to the value of the associative array

     Krsort() - sorts the array in descending order according to the keys of the associative array

Sort() - sorts the array in ascending order


The following example sorts the elements in the $cars array in ascending alphabetical order:

Instance

<?php
 $cars=array("Volvo","BMW","Toyota");
 Sort($cars);
 ?>
The following example arranges the elements in the $numbers array in ascending numerical order:

Instance
<?php 
$numbers=array(4,6,2,22,11);
 Sort($numbers);
 ?>
Rsort() - sorts the array in descending order

The following example sorts the elements in the $cars array in descending alphabetical order:

Instance

<?php 
 $cars=array("Volvo","BMW","Toyota");
 Rsort($cars);
 ?>
The following example sorts the elements in the $numbers array in descending order of numbers:

Instance
<?php
 $numbers=array(4,6,2,22,11);
 Rsort($numbers);
 ?>
Asort() - ascending the array according to the value of the array

The following example sorts the associative arrays in ascending order based on the values ​​of the array:

Instance
<?php
 $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
 Asort($age);
 ?>
Ksort() - sorts the array in ascending order based on the keys of the array

The following example sorts the associative arrays in ascending order based on the keys of the array:

Instance

 <?php
 $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
 Ksort($age);
 ?>
Arsort() - sorts the array in descending order based on the value of the array

The following example sorts the associative arrays in descending order based on the values ​​of the array:

Instance

 <?php
 $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
 Arsort($age);
 ?>
Krsort() - sorts the array in descending order according to the keys of the array

The following example sorts the associative arrays in descending order based on the keys of the array:

Instance

 <?php
 $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
 Krsort($age);
 ?>

Last Updated: 2018-07-06 12:32:44 By akshay

SHARE THIS

share_facebook share_twitter share_google_plus

Please Comment Here :