The following function are used for sorting arrays.
sort() function : The sort() function is used for sorting the value of the indexed array in ascending order.
Example :
Output :
rsort() function : The rsort() function is used for sorting the value of the indexed array in descending order.
Example :
Output :
asort() function : The asort() function sorts the elements of an associative array in ascending order according to the value.
Example :
Output :
ksort() function : The ksort() function sorts the elements of an associative array in ascending order by their keys.
Example :
Output :
arsort() function : The arsort() function sorts the elements of an associative array in descending order according to the value.
Example :
Output :
krsort() function : The krsort() function sorts the elements of an associative array in descending order by their keys.
Example :
Output :
- sort()
- rsort()
- asort()
- ksort()
- arsort()
- krsort()
sort() function : The sort() function is used for sorting the value of the indexed array in ascending order.
Example :
<?php
// Define array
$numbers = array(6,2,9,4,5);
// Sorting and printing array
sort($numbers);
print_r($numbers);
?>
// Define array
$numbers = array(6,2,9,4,5);
// Sorting and printing array
sort($numbers);
print_r($numbers);
?>
Output :
Array ( [0] => 2 [1] => 4 [2] => 5 [3] => 6 [4] => 9 )
rsort() function : The rsort() function is used for sorting the value of the indexed array in descending order.
Example :
<?php
// Define array
$numbers = array(6,2,9,4,5);
// Sorting and printing array
rsort($numbers);
print_r($numbers);
?>
// Define array
$numbers = array(6,2,9,4,5);
// Sorting and printing array
rsort($numbers);
print_r($numbers);
?>
Output :
Array
(
[0] => 9
[1] => 6
[2] => 5
[3] => 4
[4] => 2
)
asort() function : The asort() function sorts the elements of an associative array in ascending order according to the value.
Example :
<?php
$age = array("Alokesh"=>"23","Rinku"=>"22","Tapan"=>"24");
asort($age);
print_r($age);
?>
$age = array("Alokesh"=>"23","Rinku"=>"22","Tapan"=>"24");
asort($age);
print_r($age);
?>
Output :
Array
(
[Rinku] => 22
[Alokesh] => 23
[Tapan] => 24
)
ksort() function : The ksort() function sorts the elements of an associative array in ascending order by their keys.
Example :
<?php
$age = array("Alokesh"=>"23","Rinku"=>"22","Tapan"=>"24");
ksort($age);
print_r($age);
?>
$age = array("Alokesh"=>"23","Rinku"=>"22","Tapan"=>"24");
ksort($age);
print_r($age);
?>
Output :
Array
(
[Alokesh] => 23
[Rinku] => 22
[Tapan] => 24
)
arsort() function : The arsort() function sorts the elements of an associative array in descending order according to the value.
Example :
<?php
$age = array("Alokesh"=>"23","Rinku"=>"22","Tapan"=>"24");
arsort($age);
print_r($age);
?>
$age = array("Alokesh"=>"23","Rinku"=>"22","Tapan"=>"24");
arsort($age);
print_r($age);
?>
Output :
Array
(
[Tapan] => 24
[Alokesh] => 23
[Rinku] => 22
)
krsort() function : The krsort() function sorts the elements of an associative array in descending order by their keys.
Example :
<?php
$age = array("Alokesh"=>"23","Rinku"=>"22","Tapan"=>"24");
krsort($age);
print_r($age);
?>
$age = array("Alokesh"=>"23","Rinku"=>"22","Tapan"=>"24");
krsort($age);
print_r($age);
?>
Output :
Array
(
[Tapan] => 24
[Rinku] => 22
[Alokesh] => 23
)
0 Comments