PHP Sorting Arrays

Sorting an array means arranging the elements of an array in various orders like alphabetical or numerical, ascending or descending order etc. There are various functions available in PHP in order to sort an array. Some of them are as follows :

sort() : sorts arrays in ascending order.
rsort() : sorts arrays in descending order.
asort() : sorts associative arrays in ascending order, according to the value.
arsort() : sorts associative arrays in descending order, according to the value.
ksort() : sorts associative arrays in ascending order, according to the key.
krsort() : sorts associative arrays in descending order, according to the key.


1. sort() :

The sort() function is used for sorting the elements of the indexed array in ascending order, which is alphabetically for letters and numerically for numbers.

Example 1 :
<?php

  $my_arr = array("HTML", "CSS", "JavaScript", "PHP", "MySQL");
  sort($my_arr);

  $len = count($my_arr);
  for($x = 0; $x < $len; $x++) {
    echo $my_arr[$x] . ", ";
  }

?>
Output :

CSS, HTML, JavaScript, MySQL, PHP,

Example 2 :
<?php

  $my_arr = array(200, 180, 100, 130, 50, 10, 80, 140);
  sort($my_arr);

  $len = count($my_arr);  
  for($x = 0; $x < $len; $x++) {
    echo $my_arr[$x] . ", ";
  }

?>
Output :

10, 50, 80, 100, 130, 140, 180, 200,


2. rsort() :

The rsort() function is used for sorting the elements of the indexed array in descending order, which is alphabetically for letters and numerically for numbers.

Example 1 :
<?php

  $my_arr = array("HTML", "CSS", "JavaScript", "PHP", "MySQL");
  rsort($my_arr);

  $len = count($my_arr);
  for($x = 0; $x < $len; $x++) {
    echo $my_arr[$x] . ", ";
  }

?>
Output :

PHP, MySQL, JavaScript, HTML, CSS,

Example 2 :
<?php

  $my_arr = array(200, 180, 100, 130, 50, 10, 80, 140);
  rsort($my_arr);

  $len = count($my_arr);  
  for($x = 0; $x < $len; $x++) {
   echo $my_arr[$x] . ", ";
  }

?>
Output :

200, 180, 140, 130, 100, 80, 50, 10,


3. asort() :

The asort() function used sorts the elements of an associative array in ascending order according to element's value.

Example 1 :
<?php

  $age = array("John"=> 25, "Sam" => 23, "Marry" => 30, "Luci" => 29);

  asort($age);
  
  foreach($age as $key => $value) {
    echo $key . " : " . $value . "<br/>";
  }

?>
Output :

Sam : 23
John : 25
Luci : 29
Marry : 30


4. arsort :

The arsort() function used sorts the elements of an associative array in descending order according to element's value.

Example :
<?php

  $age = array("John"=> 25, "Sam" => 23, "Marry" => 30, "Luci" => 29);

  arsort($age);

  foreach($age as $key => $value) {
    echo $key . " : " . $value . "<br/>";
  }

?>
Output :

Marry : 30
Luci : 29
John : 25
Sam : 23


5. ksort() :

The ksort() function used sorts the elements of an associative array in descending order according to element's key.

Example :
<?php

  $age = array("John"=> 25, "Sam" => 23, "Marry" => 30, "Luci" => 29);

  ksort($age);

  foreach($age as $key => $value) {
    echo $key . " : " . $value . "<br/>";
  }

?>
Output :

John : 25
Luci : 29
Marry : 30
Sam : 23


6. krsort() :

The krsort() function used sorts the elements of an associative array in descending order according to element's key.

Example :
<?php

  $age = array("John"=> 25, "Sam" => 23, "Marry" => 30, "Luci" => 29);

  krsort($age);

  foreach($age as $key => $value) {
    echo $key . " : " . $value . "<br/>";
  }

?>
Output :

Sam : 23
Marry : 30
Luci : 29
John : 25


Next Topic :