Friday, January 30, 2026
HomeLanguagesSorting Arrays in PHP 5

Sorting Arrays in PHP 5

What is sorting?
Sorting refers to ordering data in an alphabetical, numerical order and increasing or decreasing fashion according to some linear relationship among the data items.Sorting greatly improves the efficiency of searching.

Sorting Functions For Arrays In PHP

  1. sort() – sorts arrays in ascending order
  2. rsort() – sorts arrays in descending order
  3. asort() – sorts associative arrays in ascending order, according to the value
  4. ksort() – sorts associative arrays in ascending order, according to the key
  5. arsort() – sorts associative arrays in descending order, according to the value
  6. krsort() – sorts associative arrays in descending order, according to the key

Sort Array in Ascending Order – sort()

The following function sorts the elements of a numerical array in ascending numerical order:

INPUT :




<!DOCTYPE html>
<html>
<body>
  
<?php
$numbers = array(40, 61, 2, 22, 13);
sort($numbers);
  
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
    echo $numbers[$x];
    echo "<br>";
}
?>
  
</body>
</html>


OUTPUT :

2
13
22
40
61

Sort Array in Descending Order – rsort()
The following function sorts the elements of a numerical array in descending numerical order:

INPUT :




<!DOCTYPE html>
<html>
<body>
  
<?php
$numbers = array(40, 61, 2, 22, 13);
rsort($numbers);
  
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
    echo $numbers[$x];
    echo "<br>";
}
?>
  
</body>
</html>


OUTPUT :

61
40
22
13
2

Sort Array in Ascending Order,According to Value – asort()
The following function sorts an associative array in ascending order, according to the value:

INPUT :




<!DOCTYPE html>
<html>
<body>
  
<?php
$age = array("ayush"=>"23", "shankar"=>"47", "kailash"=>"41");
asort($age);
  
foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
?>
  
</body>
</html>


OUTPUT :

Key=Ayush, Value=23
Key=Kailash, Value=41
Key=Shankar, Value=47

Sort Array in Ascending Order, According to Key – ksort()
The following function sorts an associative array in ascending order, according to the key:

INPUT :




<!DOCTYPE html>
<html>
<body>
  
<?php
$age = array("ayush"=>"23", "shankar"=>"47", "kailash"=>"41");
ksort($age);
  
foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
?>
  
</body>
</html>


OUTPUT :

Key=Ayush, Value=23
Key=Kailash, Value=41
Key=Shankar, Value=47

Sort Array in Descending Order, According to Value – arsort()
The following function sorts an associative array in descending order, according to the value.

INPUT :




<!DOCTYPE html>
<html>
<body>
  
<?php
$age = array("ayush"=>"23", "shankar"=>"47", "kailash"=>"41");
arsort($age);
  
foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
?>
  
</body>
</html>


OUTPUT :

Key=Shankar, Value=47
Key=Kailash, Value=41
Key=Ayush, Value=23

Sort Array in Descending Order, According to Key – krsort()
The following function sorts an associative array in descending order, according to the key.

INPUT :




<!DOCTYPE html>
<html>
<body>
  
<?php
$age = array("ayush"=>"23", "shankar"=>"47", "kailash"=>"41");
krsort($age);
  
foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
?>
  
</body>
</html>


OUTPUT :

Key=Shankar, Value=47
Key=Kailash, Value=41
Key=Ayush, Value=23

RELATED ARTICLES

Most Popular

Dominic
32478 POSTS0 COMMENTS
Milvus
122 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12065 POSTS0 COMMENTS
Shaida Kate Naidoo
6987 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6917 POSTS0 COMMENTS