Below are the php sort functions use for sorting an array in php.
- sort() - This sort function is used for sorting an array in ascending order in php.
- rsort() - This sort function is used for sorting an array in descending order in php.
- asort() -This function is used for sorting associative arrays in ascending order, with respect to value.
- ksort() -This function is used for sorting associative arrays in ascending order, with respect to the key.
- arsort() - This function is used for sorting associative arrays in descending order, with respect to value.
- krsort() - This function is used for sorting associative arrays in descending order, with respect to the key.
sort()
Below is the example of sort() function :-
<?php
$vehicle = array("Truck", "Car", "Bike");
sort($vehicle);
?>
Output
Bike
Car
Truck
rsort()
Below is the example of rsort() function :-
<?php
$vehicle = array("Truck", "Car", "Bike");
rsort($vehicle);
?>
Output
Truck
Car
Bike
asort()
Below is the example of asort() function :-
<?php
$age = array("Suresh"=>"35", "Pankaj"=>"27", "Akash"=>"33");
asort($age);
?>
Output
Pankaj 27
Akash 33
Suresh 53
ksort()
below is the example of ksort() function:-
<?php
$age = array("Suresh"=>"53", "Pankaj"=>"27", "Akash"=>"33");
ksort($age);
?>
Output
Akash 33
Pankaj 27
Suresh 53
Here key is Akash,Pankaj,Suresh and value is 33,27,53.
arsort()
Below is the arsort() example:-
<?php
$age = array("Suresh"=-->"53", "Pankaj"=>"27", "Akash"=>"33");
arsort($age);
?>
Output
Suresh 53
Akash 33
Pankaj 27
krsort()
Below is the krsort() example:-
<?php
$age = array("Suresh"=>"53", "Pankaj"=>"27", "Akash"=>"33");
krsort($age);
?>
Output
Suresh 53
Akash 33
Pankaj 27
0 Comment(s)