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…..

Comments

  1. This is one of the best answer so far, I have read online. No crap, just useful

    information. Very well presented. I had found another good collection of php interview

    question and answer over internet.
    please check out this link...

    http://www.mindstick.com/Interview/1065/What%20are%20different%20sorting%20function%20of%20array%20in%20php

    ReplyDelete

Post a Comment

Popular Posts