Sorting Functions in PHP

There are different sorting functions used to sort the arrays and other variables. Some of the their functions are listed below:

sort() – used to sort an array in normal order

rsort() – used to sort an array in normal reverse order

asort() – used to sort array and maintain the index association(key value pair maintains even after sorting)

arsort() – same as asort() but in reverse order

ksort() – used to sort an array by its keys….

Example Program

<?php

$fruits[0]=”Orange”;

$fruits[1]=”Mango”;

$fruits[2]=”Banana”;

print_r($fruits);

sort($fruits);

print_r($fruits);

?>

The above script displays the fruits array in normal order and also in sorted order…. simply replace the sort function with other sort functions and may see the results.

for key value, change the fruits array like the following script

$fruits=array(10=> “Orange”, 100=> “Mango”, 1000=>”Banana”);

and use the functions ksort, asort and arsort () and see that the key value pair is unchanged…..

0 comments:

Post a Comment