In the previous tutorials, we have discussed how to create an array in php and different types of arrays in php. Now, in this tutorials we are going to learn different types of php array sort techniques.
PHP Array Sort :
Sorting arrays provides a way to organize data sets in either alphabetical order or in numbers, in an ascending or descending manner.
PHP provides many predefined functions to sort an array. Here are the list.
- sort() – It will sort an given array in ascending order.
- rsort() – It will sort an given arrays in descending order
- asort() – It will sort an given associative array in ascending order, according to the value
- ksort() – It will sort an given associative array in ascending order, according to the key
- arsort() – It will sort an given associative array in descending order, according to the value
- krsort() – It will sort an given associative array in descending order, according to the key
PHP Array Sort Ascending order – sort() :
The sort()
is a default method given by PHP. It is used to sort the given array in ascending order.
<?php
$marks[0] = "English";
$marks[1] = "Math";
$marks[2] = "Geography";
$marks[3] = "Science";
echo "Cara loves to study. " . $marks[0] . " and " . $marks[1] . " are her favorite subjects</br>";
echo "She studies a total of " . count($marks) . " subjects. They are: </br>";
$length = count($marks);
for ($i = 0; $i < $length; $i++) {
sort($marks);
echo $marks[$i];
echo "</br>";
}
?>
Output:
Cara loves to study. English and Math are her favorite subjects
She studies a total of 4 subjects. They are:
English
Geography
Math
Science
Notice that the subjects are now organized in alphabetical order.
PHP Array Sort Descending order – rsort() :
The rsort()
is a default method given by PHP. It is used to sort the given array in descending order.
<?php $marks[0] = "English";
$marks[1] = "Math";
$marks[2] = "Geography";
$marks[3] = "Science";
echo "Cara loves to study. " . $marks[0] . " and " . $marks[1] . " are her favorite subjects</br>";
echo "She studies a total of " . count($marks) . " subjects. They are: </br>";
$length = count($marks);
for ($i = 0; $i < $length; $i++) {
rsort($marks);
echo $marks[$i];
echo "</br>";
} ?>
Output:
Cara loves to study. English and Math are her favorite subjects
She studies a total of 4 subjects. They are:
Science
Math
Geography
English
Notice that the subjects are now organized in alphabetical as descending order.
PHP Array Sort Ascending order – asort() :
The asort()
is a default method given by PHP. It is used to sort the given associative array in ascending order, according to the value.
<?php $marks['English'] = "91";
$marks['Math'] = "87";
$marks['Geography'] = "68";
$marks['Science'] = "99";
echo "Cara got " . $marks['English'] . " in English and " . $marks['Science'] . " in Science.</br>";
asort($marks);
foreach ($marks as $x => $x_value) {
echo "Subject=" . $x . ", Marks=" . $x_value . "</br>";
} ?>
Output:
Cara got 91 in English and 99 in Science.
Subject=Geography, Marks=68
Subject=Math, Marks=87
Subject=English, Marks=91
Subject=Science, Marks=99
PHP Array Sort Descending order – arsort() :
The arsort()
is a default method given by PHP. It is used to sort the given associative array in descending order, according to the value.
<?php $marks['English'] = "91";
$marks['Math'] = "87";
$marks['Geography'] = "68";
$marks['Science'] = "99";
echo "Cara got " . $marks['English'] . " in English and " . $marks['Science'] . " in Science.</br>";
arsort($marks);
foreach ($marks as $x => $x_value) {
echo "Subject=" . $x . ", Marks=" . $x_value . "</br>";
} ?>
Output:
Cara got 91 in English and 99 in Science.
Subject=Science, Marks=99
Subject=English, Marks=91
Subject=Math, Marks=87
Subject=Geography, Marks=68
PHP Array Sort Ascending order – ksort() :
The ksort()
is a default method given by PHP. It is used to sort the given associative array in ascending order, according to the key.
<?php $marks['English'] = "91";
$marks['Math'] = "87";
$marks['Geography'] = "68";
$marks['Science'] = "99";
echo "Cara got " . $marks['English'] . " in English and " . $marks['Science'] . " in Science.</br>";
ksort($marks);
foreach ($marks as $x => $x_value) {
echo "Subject=" . $x . ", Marks=" . $x_value . "</br>";
} ?>
Output:
Cara got 91 in English and 99 in Science.
Subject=English, Marks=91
Subject=Geography, Marks=68
Subject=Math, Marks=87
Subject=Science, Marks=99
PHP Array Sort Descending order – krsort() :
The krsort()
is a default method given by PHP. It is used to sort the given associative array in descending order, according to the key.
<?php $marks['English'] = "91";
$marks['Math'] = "87";
$marks['Geography'] = "68";
$marks['Science'] = "99";
echo "Cara got " . $marks['English'] . " in English and " . $marks['Science'] . " in Science.</br>";
krsort($marks);
foreach ($marks as $x => $x_value) {
echo "Subject=" . $x . ", Marks=" . $x_value . "</br>";
} ?>
Output:
Cara got 91 in English and 99 in Science.
Subject=Science, Marks=99
Subject=Math, Marks=87
Subject=Geography, Marks=68
Subject=English, Marks=91
Happy Learning 🙂