PHP Array Sort Techniques | sort() rsort() asort() arsort() ksort() krsort() TutorialsPoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
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.
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
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.
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.
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
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
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
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 🙂

How to Sort ArrayList in Java Descending Order
Using Array in AngularJs Example
How to sort a Map using Java8
PHP Multidimensional Arrays Example
Bubble Sort In Java
Java Insertion Sort
Java 8 Stream Filter Example with Objects
Selection Sort In Java
PHP Array Example Tutorials
How to Sort an Array in Parallel in Java 8
PHP Introduction Tutorials
PHP Syntax Example Tutorials
PHP Operators Example Tutorials
PHP Variables Example Tutorials
PHP Functions Example Tutorials

How to Sort ArrayList in Java Descending Order
Using Array in AngularJs Example
How to sort a Map using Java8
PHP Multidimensional Arrays Example
Bubble Sort In Java
Java Insertion Sort
Java 8 Stream Filter Example with Objects
Selection Sort In Java
PHP Array Example Tutorials
How to Sort an Array in Parallel in Java 8
PHP Introduction Tutorials
PHP Syntax Example Tutorials
PHP Operators Example Tutorials
PHP Variables Example Tutorials
PHP Functions Example Tutorials