Sort Functions For Arrays


In this chapter, we will go through the following PHP array sort functions:

sort() - sort arrays in ascending order


Example

<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
?> 

rsort() - sort arrays in descending order


<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
?>

asort() - sort associative arrays in ascending order, according to the value

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
?>

ksort() - sort associative arrays in ascending order, according to the key

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
?>

arsort() - sort associative arrays in descending order, according to the value

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
arsort($age);
?>

krsort() - sort associative arrays in descending order, according to the key

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
?>

No comments:

| Designed by Harhiktech