What are the different functions in sorting an array?

Posted on

Sorting functions in PHP:
asort()-Sort an associative array in ascending order, according to the value:

<?php
$age=array(“Peter”=>”35″,”Ben”=>”37″,”Joe”=>”43”);
asort($age);
?>
arsort()-Sort an array in reverse order and maintain index association
ksort()-Sort an array by key
krsort()-Sort an array by key in reverse order
uksort()-Sort an array by keys using a user-defined comparison function
sort()-Sort the elements of the $cars array in ascending alphabetical order:
natsort()- Sort an array using a “natural order” algorithm
rsort()-Sort the elements of the $cars array in descending alphabetical order:

<?php
$cars=array(“Volvo”,”BMW”,”Toyota”);
rsort($cars);

$clength=count($cars);
for($x=0;$x<$clength;$x++)
{
echo $cars[$x];
echo “<br>”;
}
?>

 

output-

Volvo
Toyota
BMW

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s