What Is Sort An Array PHP?
Sorting means putting information in alphabetical or numerical order, or in a way that shows an increasing or decreasing relationship between the pieces of information.
Sorting makes searching much more effective.
Sort An Array in PHP – Function Attributes
In our last lesson, we learned the basics of PHP arrays, such as what arrays are, how to create them, how to see their structure, how to get to their elements, etc.
Arrays let you do even more, like putting the elements in any order you want.
PHP has a number of built-in functions for sorting array elements in different ways, such as by alphabet, by number in ascending or descending order, and so on.
Here, we’ll look at some of the most common functions used to sort arrays.
The following Sorting function attributes below are based on the PHP official manual documentation:
Function name | Sorts by | Maintains key association | Order of sort |
---|---|---|---|
array_multisort() | value | string keys yes, int keys no | first array or sort options |
asort() | value | yes | ascending |
arsort() | value | yes | descending |
krsort() | key | yes | descending |
ksort() | key | yes | ascending |
natcasesort() | value | yes | natural, case insensitive |
natsort() | value | yes | natural |
rsort() | value | no | descending |
shuffle() | value | no | random |
sort() | value | no | ascending |
uasort() | value | yes | user-defined |
uksort() | key | yes | user-defined |
usort() | value | no | user-defined |
Sorting Indexed Arrays in Ascending Order
Using the sort()
function, an indexed array can be sorted into ascending order (alphabetically for letters and numerically for numbers).
Example 1:
<?php
// <pre> is used to have new line in array
echo("<pre>");
// Defining an array
$car_manufacturer = array("Honda", "Volkswagen", "Chevrolet", "Toyota", "Hyundai");
// Sorting an array and printing it
sort($car_manufacturer);
print_r($car_manufacturer);
echo("</pre>");
?>
According to the code above, the <pre>
tag is used to read the array conveniently and neatly.
Without the <pre>
tag, the outcome is a one-line array that is difficult to understand and messy.
The results of using the print_r()
function is as follows:
Array
(
[0] => Chevrolet
[1] => Honda
[2] => Hyundai
[3] => Toyota
[4] => Volkswagen
)
Similarly, you can sort the array’s numerical elements in ascending order.
Example 2:
<?php
// <pre> is used to have new line in array
echo("<pre>");
// Defining an array
$numbers = array(6, 35, 88, 2, 67, 76);
// Sorting an array and printing it
sort($numbers);
print_r($numbers);
echo("</pre>");
?>
The results of using the print_r()
function is as follows:
Array
(
[0] => 2
[1] => 6
[2] => 35
[3] => 67
[4] => 76
[5] => 88
)
Sorting Indexed Arrays In Descending Order
Using the rsort()
function, an indexed array can be sorted into descending order (alphabetically for letters and numerically for numbers).
Example 1:
<?php
// <pre> is used to have new line in array
echo("<pre>");
// Defining an array
$car_manufacturer = array("Honda", "Volkswagen", "Chevrolet", "Toyota", "Hyundai");
// Sorting an array and printing it
rsort($car_manufacturer);
print_r($car_manufacturer);
echo("</pre>");
?>
The results of using the print_r()
function is as follows:
Array
(
[0] => Volkswagen
[1] => Toyota
[2] => Hyundai
[3] => Honda
[4] => Chevrolet
)
Similarly, you can sort the array’s numerical elements in descending order.
Example 2:
<?php
// <pre> is used to have new line in array
echo("<pre>");
// Defining an array
$numbers = array(6, 35, 88, 2, 67, 76);
// Sorting an array and printing it
rsort($numbers);
print_r($numbers);
echo("</pre>");
?>
The results of using the print_r()
function is as follows:
Array
(
[0] => 88
[1] => 76
[2] => 67
[3] => 35
[4] => 6
[5] => 2
)
Sorting Associative Arrays In Ascending Order By Value
According to their value, the asort()
function sorts the elements of an associative array in ascending order.
It operates identically to sort()
, but keeps the association between keys and their values.
Example 1:
<?php
// <pre> is used to have new line in array
echo("<pre>");
// Defining an array
$salary = array("Bebot"=>50000, "John"=>45000, "Gabriel"=>47000, "Ludwig"=>52000);
// Sorting an array and printing it
asort($salary);
print_r($salary);
echo("</pre>");
?>
The results of using the print_r()
function is as follows:
Array
(
[John] => 45000
[Gabriel] => 47000
[Bebot] => 50000
[Ludwig] => 52000
)
Similarly, you can sort the numerical elements of an array in ascending order.
Example 2:
<?php
// <pre> is used to have new line in array
echo("<pre>");
// Defining an array
$id_info = array(19951214=>"Prince", 19960106=>"Grace", 19980823=>"Nymheria", 19921116=>"Ludylim");
// Sorting an array and printing it
asort($id_info);
print_r($id_info);
echo("</pre>");
?>
The results of using the print_r()
function is as follows:
Array
(
[19960106] => Grace
[19921116] => Ludylim
[19980823] => Nymheria
[19951214] => Prince
)
Sorting Associative Arrays In Descending Order By Value
The arsort()
function sorts the values of the elements in an associative array so that they go from highest to lowest.
It works the same way as rsort()
but keeps the link between the keys and their values while sorting.
Example 1:
<?php
// <pre> is used to have new line in array
echo("<pre>");
// Defining an array
$salary = array("Bebot"=>50000, "John"=>45000, "Gabriel"=>47000, "Ludwig"=>52000);
// Sorting an array and printing it
arsort($salary);
print_r($salary);
echo("</pre>");
?>
The results of using the print_r()
function is as follows:
Array
(
[Ludwig] => 52000
[Bebot] => 50000
[Gabriel] => 47000
[John] => 45000
)
Similarly, you can sort the numerical elements of an array in descending order.
Example 2:
<?php // <pre> is used to have new line in array echo("<pre>"); // Defining an array $id_info = array(19951214=>"Prince", 19960106=>"Grace", 19980823=>"Nymheria", 19921116=>"Ludylim"); // Sorting an array and printing it arsort($id_info); print_r($id_info); echo("</pre>"); ?>
The results of using the print_r()
function is as follows:
Array
(
[19951214] => Prince
[19980823] => Nymheria
[19921116] => Ludylim
[19960106] => Grace
)
Sorting Associative Arrays In Ascending Order By Key
The ksort()
function sorts the keys of the elements in an associative array so that they are in ascending order.
It works the same as the asort()
function in that it keeps the link between the keys and their values while sorting.
Example 1:
<?php
// <pre> is used to have new line in array
echo("<pre>");
// Defining an array
$salary = array("Bebot"=>50000, "John"=>45000, "Gabriel"=>47000, "Ludwig"=>52000);
// Sorting an array and printing it
ksort($salary);
print_r($salary);
echo("</pre>");
?>
The results of using the print_r()
function is as follows:
Array
(
[Bebot] => 50000
[Gabriel] => 47000
[John] => 45000
[Ludwig] => 52000
)
Similarly, you can sort the numerical elements of an array in ascending order.
<?php
// <pre> is used to have new line in array
echo("<pre>");
// Defining an array
$id_info = array(19951214=>"Prince", 19960106=>"Grace", 19980823=>"Nymheria", 19921116=>"Ludylim");
// Sorting an array and printing it
ksort($id_info);
print_r($id_info);
echo("</pre>");
?>
The results of using the print_r()
function is as follows:
Array
(
[19921116] => Ludylim
[19951214] => Prince
[19960106] => Grace
[19980823] => Nymheria
)
Sorting Associative Arrays In Descending Order By Key
The krsort()
function sorts the keys of the elements in an associative array so that they are in decreasing order.
It works the same as the arsort()
function in that it keeps the link between keys and their values while sorting.
Example 1:
<?php
// <pre> is used to have new line in array
echo("<pre>");
// Defining an array
$salary = array("Bebot"=>50000, "John"=>45000, "Gabriel"=>47000, "Ludwig"=>52000);
// Sorting an array and printing it
krsort($salary);
print_r($salary);
echo("</pre>");
?>
The results of using the print_r()
function is as follows:
Array
(
[Ludwig] => 52000
[John] => 45000
[Gabriel] => 47000
[Bebot] => 50000
)
Similarly, you can sort the numerical elements of an array in descending order.
<?php
// <pre> is used to have new line in array
echo("<pre>");
// Defining an array
$id_info = array(19951214=>"Prince", 19960106=>"Grace", 19980823=>"Nymheria", 19921116=>"Ludylim");
// Sorting an array and printing it
krsort($id_info);
print_r($id_info);
echo("</pre>");
?>
The results of using the print_r()
function are as follows:
Array
(
[19980823] => Nymheria
[19960106] => Grace
[19951214] => Prince
[19921116] => Ludylim
)
Related Articles
- Array Push PHP (With Detailed Explanation)
- PHP array_merge With Detailed Explanation
- PHP Array Iterator with Example Program
- PHP Glob() Function With Examples
Summary
In summary, we learned how to sort an array in PHP using example programs that helped us understand the different functions.
If you want to learn more about sort an array in PHP, please leave a comment below. We’ll be happy to hear it!