Sorting Arrays

The elements in an array can be sorted in alphabetical or numerical order, descending or ascending.

 

PHP - Sort Functions For Arrays

In this chapter, we will go through the following PHP array sort functions:

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

 

Sort Array in Ascending Order – sort()

The following function sorts the elements of a numerical array in ascending numerical order:

Example:

<?php
  $numbers = array(40, 61, 2, 22, 13);
  sort($numbers);

  $arrlength = count($numbers);
  for($x = 0; $x < $arrlength; $x++) {
      echo $numbers[$x];
      echo "<br>";
  }
?>

Output:

2
13
22
40
61

 

Sort Array in Descending Order – rsort()

The following function sorts the elements of a numerical array in descending numerical order:

Example:

<?php
  $numbers = array(40, 61, 2, 22, 13);
  rsort($numbers);

  $arrlength = count($numbers);
  for($x = 0; $x < $arrlength; $x++) {
      echo $numbers[$x];
      echo "<br>";
  }
?>

Output:

61
40
22
13
2

 

Sort Array in Ascending Order,According to Value – asort()

The following function sorts an associative array in ascending order, according to the value:

Example:

<?php
  $age = array("ayush"=>"23", "shankar"=>"47", "kailash"=>"41");
  asort($age);

  foreach($age as $x => $x_value) {
      echo "Key = " . $x . ", Value = " . $x_value;
      echo "<br>";
  }
?>

Output:

Key = ayush, Value = 23
Key = kailash, Value = 41
Key = shankar, Value = 47

 

Sort Array in Ascending Order, According to Key – ksort()

The following function sorts an associative array in ascending order, according to the key:

Example:

<?php
  $age = array("ranjeet"=>"23", "hemant"=>"47", "kailash"=>"41");
  ksort($age);

  foreach($age as $x => $x_value) {
      echo "Key = " . $x . ", Value = " . $x_value;
      echo "<br>";
  }
?>

Output:

Key = hemant, Value = 47
Key = kailash, Value = 41
Key = ranjeet, Value = 23

 

Sort Array in Descending Order, According to Value – arsort()

The following function sorts an associative array in descending order, according to the value.

Example:

<?php
  $age = array("hemant"=>"23", "ranjeet"=>"47", "kailash"=>"41");
  arsort($age);

  foreach($age as $x => $x_value) {
      echo "Key = " . $x . ", Value = " . $x_value;
      echo "<br>";
  }
?>

Output:

Key = ranjeet, Value = 47
Key = kailash, Value = 41
Key = hemant, Value = 23

 

Sort Array in Descending Order, According to Key – krsort()

The following function sorts an associative array in descending order, according to the key.

Example:

<?php
  $age = array("ranjeet"=>"23", "hemant"=>"47", "kailash"=>"41");
  krsort($age);

  foreach($age as $x => $x_value) {
      echo "Key = " . $x . ", Value = " . $x_value;
      echo "<br>";
  }
?>

Output:

Key = ranjeet, Value = 23
Key = kailash, Value = 41
Key = hemant, Value = 47