PHP

Sorting Arrays in PHP

Introduction
Sorting is a process by which we can arrange the elements of a list in a specific order i.e. ascending or descending order. We can say sorting is the process of putting a list or a group of items in a specific order. Sorting may be alphabetical or numerical.
In PHP, sorting is a process of arranging the elements of an array in a specific order i.e. ascending or descending order. Using sorting you can analyze a list in a more effective way. You can sort an array by value, key or randomly. In the most situations we need to sort an array by value.
Some sorting functions are as follows:

  • sort()
  • asort()
  •  rsort()
  •  arsort()

sort() function
Using the sort() function you can sort easily and simply. The example of the sort() function is as follows:

<html>
<body bgcolor="pink">
<h3>Sort() Function</h3>
<?php
 $name=array ("Anuj","Sanjeev","Manish","Rahul");
 echo "<b>Before sorting the values are:</b>".'<br>';
 print_r($name).'<br>'.'<br>';
 sort($name);
 echo"<br/>";
 echo"<br/>";
 echo "<b>After sorting values become:</b>".'<br>';
 print_r($name).'<br>';
?>
</body>
</html>

In the above example we defined four elements (names) in the array. In this example we used the sort() function. The sort() function arranges the elements of an array in ascending (alphabetically) order. You can see in the following image:
sort
asort() function
In the example above you saw that the elements of the array are arranged in ascending (alphabetically) order. The asort() function sorts an array and also maintains the index position. An example of the asort() function is as follows:

<html>
<body bgcolor="pink">
<h3>Sort() Function</h3>
<?php
 $name=array ("Anuj","Sanjeev","Manish","Rahul");
 echo "<b>Before sorting the values are:</b>".'<br>';
 print_r($name).'<br>'.'<br>';
 asort($name);
 echo"<br/>";
 echo"<br/>";
 echo "<b>After sorting values become:</b>".'<br>';
 print_r($name).'<br>';
?>
</body>
</html>

Output — In this output you will see the array sorted by their index position.
asort
rsort() function
The rsort() function is used to sort an array in descending (alphabetic) order. An example of the rsort() function is as follows:

<html>
<body bgcolor="pink">
<h3>Sort() Function</h3>
<?php
$name=array ("Anuj","Sanjeev","Manish","Rahul");
 echo "<b>Before sorting the values are:</b>".'<br>';
 print_r($name).'<br>'.'<br>';
 rsort($name);
 echo"<br/>";
 echo"<br/>";
 echo "<b>After sorting values become:</b>".'<br>';
 print_r($name).'<br>';
?>
</body>
</html>

Output— In this output you will see the array sorted in a descending (alphabetically) order.
rsort
arsort() function
The arsort() function is a combination of asort() + rsort(). The arsort() function will sort an array in reverse order and maintain index position. The example of the arsort() function is as follows:

<html>
<body bgcolor="pink">
<h3>Sort() Function</h3>
<?php
$name=array ("Anuj","Sanjeev","Manish","Rahul");
echo "<b>Before sorting the values are:</b>".'<br>';
print_r($name).'<br>'.'<br>';
arsort($name);
echo"<br/>";
echo"<br/>";
echo "<b>After sorting values become:</b>".'<br>';
print_r($name).'<br>';
?>
</body>
</html>

 
arsort
Conclusion
In this article you saw how to sort an array in PHP. Using this article one can easily understand sorting an arrays in PHP.

Recommended Tutorials for you


Related posts

Session In PHP

Anuj Kumar

Time Ago Script

Anuj Kumar

Captcha Image Verification

Anuj Kumar

How to get Current Indian time in PHP

Anuj Kumar

Password Hashing in PHP

Anuj Kumar

Get City Country By IP Address in PHP

Anuj Kumar

Adding Comments to PHP Code

Anuj Kumar

COOKIES IN PHP

Anuj Kumar

Looping Statement In PHP

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.