How to Sort Array in PHP [Explained With Examples]

Sometimes we need to sort the data of an array in either ascending order or descending order. It is always helpful to sort the array data for searching specific data from a group of data.

There are two types of sorting, such as in ascending order or in descending order. PHP provides inbuilt functions for sorting the data as per alphabets or numbers.

Sorting Methods in PHP

  • sort() – used for sorting arrays in ascending order
  • rsort() – used for sorting arrays in descending order
  • asort() – used for sorting an associative array in ascending order according to the value
  • ksort() – used for sorting an associative array in ascending order according to the key
  • arsort() – used for sorting an associative array in descending order according to the value
  • krsort() – used for sorting an associative array in descending order according to the key

Read Also: List of PHP Date/Time Functions With Examples

1] The sort() function

This function is used for sorting array in ascending order.

Example 1

Suppose we have an array of fruits, and we want to sort it in ascending order.

<!DOCTYPE html>
<html>
<body>

<?php
$fruits = array("Mango", "Apple", "Orange", "Banana");
sort($fruits);
echo "Sorted array in ascending order:<br>";
$length = count($fruits);
for($m = 0; $m < $length; $m++) {
echo $fruits[$m];
echo "<br>";
}
?>

</body>
</html>

Output

Sorted array in ascending order:
Apple
Banana
Mango
Orange

Explanation: Here we use sort() function to sort the array in ascending order and then we use for loop to print the sorted array.

Example 2

Let’s consider, we have an array of scores of a cricketer for five matches, and we want to sort it in ascending order.

<!DOCTYPE html>
<html>
<body>

<?php
$score = array(46,24,107,02,76);
sort($score);
echo "Sorted array in ascending order:<br>";
$length = count($score);
for($m = 0; $m < $length; $m++) {
echo $score[$m];
echo "<br>";
}
?>

</body>
</html>

Output

Sorted array in ascending order:
2
24
46
76
107

2] The rsort() function

This function is used for sorting array in descending order.

Example 1

Now we have an array of fruits, and we want to sort it in descending order.

<!DOCTYPE html>
<html>
<body>

<?php
$fruits = array("Mango", "Apple", "Orange", "Banana");
rsort($fruits);
echo "Sorted array in desending order:<br>";
$length = count($fruits);
for($m = 0; $m < $length; $m++) {
echo $fruits[$m];
echo "<br>";
}
?>

</body>
</html>

Output

Sorted array in descending order:
Orange
Mango
Banana
Apple

Explanation: Here we use rsort() function to sort the array in descending order and then we use for loop to print the sorted array.

Example 2

Imagine we have an array of scores of a cricketer in five matches, and we want to sort it in descending order.

<!DOCTYPE html>
<html>
<body>

<?php
$score = array(46,24,107,02,76);
rsort($score);
echo "Sorted array in descending order:<br>";
$length = count($score);
for($m = 0; $m < $length; $m++) {
echo $score[$m];
echo "<br>";
}
?>

</body>
</html>

Output

Sorted array in descending order:
107
76
46
24
2

3] The asort() function

This function is used for sorting associative arrays in ascending order according to the value.

Example

we have an associative array of students having the Name as a key and roll number as value, and we want to sort the array in ascending order as per the roll number.

<!DOCTYPE html>
<html>
<body>

<?php
$student = array("Peter"=>"10", "Ben"=>"37", "Joe"=>"54");
asort($student);
echo "Sorted array in ascending order as per the Roll number as value:<br>";
foreach($student as $m => $m_val) {
echo "Name:" . $m . " | Roll_number:" .$m_val;
echo "<br>";
}
?>

</body>
</html>

Output

Sorted array in ascending order as per the Roll number as value:
Name:Peter | Roll_number:10
Name:Ben | Roll_number:37
Name:Joe | Roll_number:54

Explanation: Here we use the asort() function for sorting the array of students in ascending order and then we use foreach loop to print the sorted array as per their roll numbers.

4] The ksort() function

This function is used for sorting associative arrays in ascending order according to the key value.

Example

Now, we have an associative array of books having Name as a key and quantity as value, and we want to sort the array in ascending order as per the book name.

<!DOCTYPE html>
<html>
<body>

<?php
$books = array("Maths"=>"10", "Science"=>"7", "Computer"=>"15");
ksort($books);
echo "Sorted array in ascending order as per the Name as key:<br>";
foreach($books as $m => $m_val) {
echo "Name:" . $m . " | Quantity:" .$m_val;
echo "<br>";
}
?>

</body>
</html>

Output

Sorted array in ascending order as per the Name as key:
Name:Computer | Quantity:15
Name:Maths | Quantity:10
Name:Science | Quantity:7

Explanation: Here we use ksort() function for sorting the array of books in ascending order and then we use foreach loop to print the sorted array as per their names as key values.

5] The arsort() function

This function is used for sorting associative arrays in descending order according to the value.

Example

We have an associative array of students having Name as a key and roll number as value, and we want to sort the array in descending order as per the roll number value.

<!DOCTYPE html>
<html>
<body>

<?php
$student = array("Peter"=>"10", "Ben"=>"37", "Joe"=>"54");
arsort($student);
echo "Sorted array in descending order as per the Roll number as value:<br>";
foreach($student as $m => $m_val) {
echo "Name: " . $m . " | Roll_number: " .$m_val;
echo "<br>";
}
?>

</body>
</html>

Output

Sorted array in descending order as per the Roll number as value:
Name: Joe | Roll_number: 54
Name: Ben | Roll_number: 37
Name: Peter | Roll_number: 10

Explanation: Here we use arsort() function for sorting the array of students in descending order and then we use foreach loop to print the sorted array as per their roll number values.

6] The krsort() function

This function is used for sorting associative arrays in descending order according to the key value.

Example

We have an associative array of books having Name as a key and quantity as value, and we want to sort the array in descending order as per the book name.

<!DOCTYPE html>
<html>
<body>

<?php
$books = array("Maths"=>"10", "Science"=>"7", "Computer"=>"15");
krsort($books);
echo "Sorted array in descending order as per the Name as key:<br>";
foreach($books as $m => $m_val) {
echo "Name: " . $m . " | Quantity: " .$m_val;
echo "<br>";
}
?>

</body>
</html>

Output

Sorted array in descending order as per the Name as key:
Name: Science | Quantity: 7
Name: Maths | Quantity: 10
Name: Computer | Quantity: 15

Explanation: Here we use krsort() function for sorting the array of books in descending order and then we use foreach loop to print the sorted array as per their names as key values.

Conclusion

This is all about sorting the array in ascending or descending order. We can use diverse methods according to the requirements and sort the array of alphabets or numbers.

I hope you found this post informative.

Thanks for reading 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *