PHP Array Sorting
PHP Array Sorting means to arrange the elements of the array in a particular(ascending or descending or alphabetically) order. PHP has some array sorting functions to sort the array.
PHP Array Sorting Functions
PHP have some built-in sorting functions for array and these functions are used to sort the elements of the array.
List of popular pre-defined Array Sorting Functions
Fuctions | Description |
---|---|
sort() | To sort an array in ascending order. |
rsort() | To sort an array in descending order. |
asort() | To sort an associative array in ascending order on the basis of values. |
ksort() | To sort an associative array in ascending order on the basis of key. |
arsort() | To sort an associative arrya in descending order on the basis of values. |
krsort() | To sort an associative arrya in descending order on the basis of key. |
sort() Function – To sort array in ascending order.
sort() function is used to sort an array in the ascending order.
Example 1 – sort() Function Code
<!DOCTYPE html> <html> <head> </head> <body> <?php $marks = array("Maths", "Physics", "Chemistry", "English"); echo $marks[0] . "<br />" . $marks[1] . "<br />" . $marks[2] . "<br />" . $marks[3] . "<br />"; echo "<br>"; sort($marks); echo "The sorted array....<br />"; $arraylength = count($marks); for($i = 0; $i < $arraylength; $i++) { echo $marks[$i]; echo "<br>"; } ?> </body> </html>
In the above example, we show how the sort() is used to sort an array.
rsort() Function – To sort array in descending order.
The rsort() function is used to sort an array in the descending order.
Example 2 – rsort() Function Code
<!DOCTYPE html> <html> <head> </head> <body> <?php $marks = array("Maths", "Physics", "Chemistry", "English"); echo $marks[0] . "<br />" . $marks[1] . "<br />" . $marks[2] . "<br />" . $marks[3] . "<br />"; echo "<br>"; rsort($marks); echo "The sorted array....<br />"; $arraylength = count($marks); for($i = 0; $i < $arraylength; $i++) { echo $marks[$i]; echo "<br>"; } ?> </body> </html>
In the above example, we show how the rsort() is used to sort an array in the descending order.
PHP asort() Function – To sort array in ascending order by values
The asort() function is used to sort an associative array in the ascending order but the array will be sort on the basis of “values”.
Example 3 – asort() Function Code
<!DOCTYPE html> <html> <head> </head> <body> <?php $marks = array("Peter" => 75, "John" => 78, "Charlie" => 99); echo $marks["Peter"] . "<br />" . $marks["John"] . "<br />" . $marks["Charlie"] . "<br />"; echo "<br>"; asort($marks); echo "The sorted array....<br />"; foreach($marks as $i => $i_value ) { echo "Key=" . $i . ", Value=" . $i_value; echo "<br>"; } ?> </body> </html>
In the above example, we show how the asort() is used to sort an array in the ascending order by their values.
PHP ksort() Function – To sort array in ascending order by key
The ksort() function is used to sort an associative array in the ascending order but the array will be sort on the basis of “key”.
Example 4 – ksort() Function Code
<!DOCTYPE html> <html> <head> </head> <body> <?php $marks = array("Peter" => 75, "John" => 78, "Charlie" => 99); foreach ($marks as $key => $value) { echo $key . "-" . $value . "<br />"; } echo "<br>"; ksort($marks); echo "The sorted array....<br />"; foreach($marks as $key => $value ) { echo $key . "-" . $value; echo "<br>"; } ?> </body> </html>
In the above example, we show how the ksort() is used to sort an array in the ascending order by their key.
arsort() Function – To sort associative array in descending order on the basis of “values”
The arsort() function is used to sort an associative array in the descending order but the array will be sort on the basis of “values”.
Example 5 – arsort() Function Code
<!DOCTYPE html> <html> <head> </head> <body> <?php $marks = array("Peter" => 75, "John" => 78, "Charlie" => 99); foreach ($marks as $key => $value) { echo $key . "-" . $value . "<br />"; } echo "<br>"; arsort($marks); echo "The sorted array....<br />"; foreach($marks as $key => $value ) { echo $key . "-" . $value; echo "<br>"; } ?> </body> </html>
In the above example, we show how the arsort() is used to sort an array in the descending order by their key.
krsort() Function – To sort associative array in descending order on the bassis of “Key”.
The krsort() function is used to sort an associative array in the descending order but the array will be sort on the basis of “key”.
Example 6 – krsort() Function Code
<!DOCTYPE html> <html> <head> </head> <body> <?php $marks = array("Peter" => 75, "John" => 78, "Charlie" => 99); foreach ($marks as $key => $value) { echo $key . "-" . $value . "<br />"; } echo "<br>"; krsort($marks); echo "The sorted array....<br />"; foreach($marks as $key => $value ) { echo $key . "-" . $value; echo "<br>"; } ?> </body> </html>
In the above example, we show how the krsort() is used to sort an array in the descending order by their key.