sorting-functions -in-php

May 2, 2013
Posted by admin 12:26 AM

In php we have different types of functions to sort the array. We can use these sorting functions when we want to display the array in some order  . These sorting functions make our task easier .Some of the important array sorting functions are:

array_multisort  :

1:  array_multisort used to sort multidimensional arrays or multiple array at a time .

2 :While ordering the array  it maintains the key if the keys are string ie. Associative array ,but it reindxes the numeric keys.

3 : It returns Boolean value. True on success and false on failure.

e.g :

 <?php

$array1 =  array(101, 130, 400, 50,600);

$array2 =  array(2, 3, 1, 5,4);

array_multisort($array1, $array2);

?>

OUTPUT :  $array1 =  array(50, 101, 130, 400, 600);

                    $array2 =  array(5, 2, 3, 1, 4);

 

asort :

Sort an array and maintain  index association .

e.g : $places = array(‘b’=>delhi,’c’=>mumbai,’q’=>chennai,’d’=>banglore);

      asort($places);

OUTPUT :   array(‘d’=>banglore,’q’=>chennai,’b’=>delhi,’c’=>mumbai);

 

arsort :

Sort an array in reverse order and maintain  index association .

e.g : $places = array(‘b’=>delhi,’c’=>mumbai,’q’=>chennai,’d’=>banglore);

      arsort($places);

OUTPUT :  array(’c’=>mumbai , ’b’=>delhi,’q’=>chennai, ‘d’=>banglore);