Thursday, 22 January 2015

Array Sorting Functions in PHP



In php we have various functions that can be used for sorting of array in alphabetical order, numerical order ascending and descending order. Well when we are having the big count multiple items in a single variable array. We will need to sort them in ascending and descending format for use in the various functions in a program. Following are the methods with definitions which are used in sorting of arrays.

1)- sort():- sort function is used for sorting the array in ascending order in alphabetical or numerical order. 

For example
<?php
$team = array("jagvinder", "dalpreet", "baljot");
sort($team);
Above array will be sorted like baljot,dalpreet, jagvinder
$num = array(4,1 6, 2, 22, 11);
sort($num);
Above array will be sorted like  2,4,11,16,22.
 
?>
2)-rsort():- this function will sort the array in descending order alphabetically and numerically
<?php
$webteam = array("harinder", "diljot", "baltaj");
rsort($webteam);
Above array will be sorted like harinder,diljot,baltaj
$num = array(17,1 5, 12, 24, 10);
rsort($num);
Above array will be sorted like  24,15,17,12,10.
  ?>
3)-asort():- This function  will sort the associative array according to the value in ascending order.
<?php
$salary = array("baldev"=>"25500", "harjot"=>"30500", "amarjeet"=>"20500");

asort($salary);
output will be
20500,25500,30500
  ?>
4)-ksort():-This function  will sort the associative array according to the key in ascending order.
<?php
$salary = array("iqbal"=>"28000", "harjeet"=>"31000", "raj"=>"17000");

ksort($salary);
output will be
31000,28000,17000
  ?>
5)-arsort():-This function  will sort the associative array according to the value in descending order.
<?php
$salary = array("harman"=>"27000", "jaswant"=>"32000", "diljot"=>"18000");

arsort($salary);
output will be
18000,27000,32000
  ?>
6)-krsort():-This function  will sort the associative array according to the key in descending order.
<?php
$salary = array("balraj"=>"26000", "joginder"=>"31000", "daljeet"=>"19000");

krsort($salary);
output will be
31000,19000,26000
  ?>

This examples of sorting of arrays is provided by the experienced trainers of webcom systems. There is much more in arrays that can be learned. So for complete guidance in arrays in php and its functions join the best industrial training in Chandigarh webcom systems.

No comments:

Post a Comment