Array Sorting In PHP

Here we will explore some of the different ways of PHP sort arrays with examples and describe some of the uses for the various methods.

PHP sort array is an essential operation, since it facilitates a better arrangement of data, as well as making it easier to retrieve it when needed.

There are several built-in functions in PHP that can sort arrays based on a variety of parameters, such as a value, key, ascending or descending order, etc.

To be able to select the most appropriate sorting method for a particular use case in PHP, You must have a thorough understanding of the different sorting methods to make the right choice.

This will enable you to improve the efficiency and performance of your PHP programs.



PHP Sort Array Functions

Here, we are going to take a look at how to sort PHP arrays by the following methods:

FunctionsOverview
sort()This is a method for sorting arrays in ascending order.
rsort()This is a method for sorting arrays in descending order.
asort()This function sorts associative arrays in ascending order based on the value they contain.
ksort()Sorts associative arrays from ascending to descending order based on the key they contain.
arsort()It is a function that sorts associative arrays in descending order based on their values.
krsort()It is a method that sorts associative arrays according to their key, in descending order.

Array Ascending Order Sort

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

PHP Sort Array Example: 1 

<?php $names = array("Jos", "Stuart", "Ben", "Tom"); sort($names); //This function sorts the array into ascending order$len=count($names); for($a=0;$a<$len;$a++) { echo $names[$a]; echo "<br>"; } ?>

PHP Sort Arrays

In the example below, we will sort the elements of the $num array in ascending numerical order as follows:

PHP Sort Array Example: 2 

<?php $numbers = array(6, 2, 11, 42, 9); sort($numbers); //This sorts the number in ascending order $len=count($numbers); for($a=0;$a<$len;$a++) { echo $numbers[$a]; echo "<br>"; } ?>

Array Descending Order Sort

It is possible to sort an array using the rsort() function in descending order based on the values contained in the array. Here’s how it works:

Example: 

<?php $names = array("Jos", "Stuart", "Ben", "Tom"); rsort($names); //This function sorts the array into descending order $len=count($names); for($a=0;$a<$len;$a++) { echo $names[$a]; echo "<br>"; } ?>
In the example below, we will sort the elements of the $num array in descending numerical order as follows:

Example: 

<?php $numbers = array(6, 2, 11, 42, 9); rsort($numbers); //This sorts the number in ascending order $len=count($numbers); for($a=0;$a<$len;$a++){ echo $numbers[$a]; echo "<br>"; } ?>

PHP Sort Array – asort()

Using PHP asort() function, you can sort an array ascendingly according to its values while maintaining the association between the keys and the values in the array while sorting.
To put it another way, the function sorts the array according to the values, but it keeps the keys paired with their respective values while it sorts.
Below is an example of sorted arrays according to their values by using the asort() function:

Example: 

<?php $marks = array( "Science"=>90, "Mathematic"=>80, "English"=>86, "Computer Studies"=>84, "Geography"=>96); asort($marks); // This sorts by the values in the key value pair foreach($marks as $mark => $value){ echo $mark." = ". $value; echo "<br>"; } ?>
Here is another example for clear understanding of the asort() function to sort the array according to its value:

Example: 

<?php $user_info = array( "FirstName"=>"Mark", "LastName"=>"Zuckerberg", "Age"=>38, "Gender"=>"M", ); asort($user_info); foreach($user_info as $field => $value) { echo $field . " = " . $value. "<br>"; echo " "; } ?>

PHP Sort Array – ksort()

Using the ksort() function, you can sort an array of data in ascending order based on the keys of the array.
By doing this, the keys in the array will ascend in the order they were sorted, and the values corresponding to those keys will remain associated with the keys of the array.
Below is an example of sorted array in ascending according to their key by using the ksort() function:

Example: 

<?php $marks = array( "Science"=>90, "Mathematic"=>80, "English"=>86, "Computer Studies"=>84, "Geography"=>96); ksort($marks); // This sorts by the values in the key value pair foreach($marks as $mark => $value){ echo $mark." = ". $value; echo "<br>"; } ?>
Here is another example for clear understanding of the ksort() function to sort the array in descending according to its key:

Example: 

<?php $user_info = array( "FirstName"=>"Mark", "LastName"=>"Zuckerberg", "Age"=>38, "Gender"=>"M", ); ksort($user_info); foreach($user_info as $field => $value) { echo $field . " = " . $value. "<br>"; echo " "; } ?>

PHP Sort Array – arsort()

Using the arsort() function, one can sort a collection in descending order based on the values contained in it, while maintaining the association between the values and keys.
Thereby, the keys that are associated with the respective values of the rows will remain associated with those values after the sort has been completed.
Below is an example of sorted array in descending according to their value by using the arsort() function:

Example: 

<?php $marks = array( "Science"=>90, "Mathematic"=>80, "English"=>86, "Computer Studies"=>84, "Geography"=>96); arsort($marks); // This sorts by the values in the key value pair foreach($marks as $mark => $value){ echo $mark." = ". $value; echo "<br>"; } ?>
Here is another example for clear understanding of the arsort() function to sort the array in descending according to its value:

Example: 

<?php $user_info = array( "FirstName"=>"Mark", "LastName"=>"Zuckerberg", "Age"=>38, "Gender"=>"M", ); arsort($user_info); foreach($user_info as $field => $value) { echo $field . " = " . $value. "<br>"; echo " "; } ?>

PHP Sort Array – krsort()

In krsort(), you can sort an array of items in descending order based on the keys contained within it.
By doing this, it means that the keys in the array will be sorted in descending order, while the values associated with each key will remain sorted in descending order.
Below is an example of sorted array in descending according to their key by using the krsort() function:

Example: 

<?php $marks = array( "Science"=>90, "Mathematic"=>80, "English"=>86, "Computer Studies"=>84, "Geography"=>96); krsort($marks); // This sorts by the values in the key value pair foreach($marks as $mark => $value){ echo $mark." = ". $value; echo "<br>"; } ?>
Here is another example for clear understanding of the krsort() function to sort the array in descending according to its key:

Example: 

<?php $user_info = array( "FirstName"=>"Mark", "LastName"=>"Zuckerberg", "Age"=>38, "Gender"=>"M", ); krsort($user_info); foreach($user_info as $field => $value) { echo $field . " = " . $value. "<br>"; echo " "; } ?>

Example Explanation

In above example we have created an associative array $user_info with some personal information about a person (in this case, Mark Zuckerberg), and then sorts the array in descending order by key using the krsort() function. Then we use a foreach loop to print the sorted array on the webpage.

This creates an associative array $user_info with four key-value pairs, where each key represents a field of personal information and each value represents a specific value of that field.

The “FirstName” key has a value of “Mark”, the “LastName” key has a value of “Zuckerberg”, and so on.

The krsort() function sorts the array $user_info in descending order by key.

This means that the array will be sorted based on the keys in reverse order, from “LastName” to “Age” to “FirstName”.

This foreach loop is used to iterate over the sorted array $user_info and print out each key-value pair on the webpage.

For each key-value pair, the code prints out the key followed by the value, separated by an equal sign and a space.

So, the final output of this code on the web page would be:

LastName = Zuckerberg
Age = 38
FirstName = Mark
Gender = M

As you can see, the array has been sorted in descending order by key, and the foreach loop is used to print out each key-value pair in that order.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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