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 :
filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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 :
filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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 :
filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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 :
filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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 :
filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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 :
filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


OUTPUT :

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



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.