List functions available to sort a PHP array. explain with example

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Array sorting functions

What is sorting?

Sorting is a way in PHP to order data in an alphabetical, numerical, and increasing or decreasing order fashion according to some linear relationship among the data items. Sorting greatly builds the efficiency of searching.

There are six Sorting functions for Arrays in PHP

  • sort() – Ascending order in sorts arrays
  • rsort() – Descending order in sorts arrays
  • asort() – Ascending order in sorts associative arrays, according to the value
  • ksort() – Ascending order in sorts associative arrays, according to the key
  • arsort() –Descending order in sorts associative arrays, according to the value
  • krsort() –Descending order in sorts associative arrays, according to the key

Ascending Order in Sort Array – sort()

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

INPUT :

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

OUTPUT :

2
13
22
40
61

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

INPUT :

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

OUTPUT :
61
40
22
13
2

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

INPUT :

<?php
$age = array("Mayur"=>"23", "Pritakh"=>"47", "Shailesh"=>"41");
asort($age);
  
foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
?>

OUTPUT :

Key=Mayur, Value=23
Key=Pritakh, Value=41
Key=Shailesh, Value=47

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

INPUT :

<?php
$age = array("Mayur"=>"23", "Pritakh"=>"47", "Shailesh"=>"41");
ksort($age);
  
foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
?>

OUTPUT :

Key=Mayur, Value=23
Key=Pritakh, Value=41
Key=Shailesh, Value=47

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

INPUT :

<?php
$age = array("Mayur"=>"23", "Pritakh"=>"47", "Shailesh"=>"41");
arsort($age);
  
foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
?>

OUTPUT :

Key=Mayur, Value=47
Key=Pritakh, Value=41
Key=Shailesh, Value=23

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

INPUT :

<?php
$age = array("Mayur"=>"23", "Pritakh"=>"47", "Shailesh"=>"41");
krsort($age);
  
foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
?>

OUTPUT :

Key=Mayur, Value=47
Key=Pritakh, Value=41
Key=Shailesh, Value=23

Related Posts

What will you learn from Laravel?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Error: MySQL shut down unexpectedly, How to solve this error on xampp?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

What is the requirement for learning Laravel?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

How to repair the corrupted table ‘global_priv’ in the localhost PHPMyAdmin database?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Whenever you’re updating your PHP form data and getting a warning: count(): error message on the above page? How can you fix it?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

If details values are not retrieving at your PHP form whenever you update/edit your form. How can you fix it?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x