The elements in an array can be either be sort ascending, descending,
alphabetical and numerical order. Lets discuss the different sort function used
in PHP.
- sort() - used to sort arrays in ascending order.
- rsort() - used to sort arrays in descending order.
- asort() - used to sort associative
arrays in ascending order as per values.
- ksort() - used to sort associative arrays in ascending order as per key.
- arsort() - used to sort associative arrays in descending order as per
values.
- krsort() - used to sort associative arrays in descending order as
per key.
- shuffle() - used to randomizes the order of the elements in an array.
<?php $players = array("Sambir","Rupal","Aadarsh"); sort($players); print_r($players); ?> Output: Array( [0] => Aadarsh [1] => Rupal [2] => Sambir )
<?php $players = array("Rupal","Sambit","Aadarsh"); rsort($players); print_r($players); ?> Output: Array( [0] => Sambir [1] => Rupal [2] => Aadarsh )
<?php $players = array("Sambir"=>"9","Rupal"=>"10","Aadarsh"=>"8"); asort($players); print_r($players); ?> Output: Array( [Aadarsh] => 8 [Sambir] => 9 [Rupal] => 10 )
<?php $players = array("Sambir"=>"9","Rupal"=>"10","Aadarsh"=>"8"); ksort($players); print_r($players); ?> Output: Array( [Aadarsh] => 8 [Rupal] => 10 [Sambir] => 9 )
<?php $players = array("Sambir"=>"9","Rupal"=>"10","Aadarsh"=>"8"); ksort($players); print_r($players); ?> Output: Array( [Aadarsh] => 8 [Rupal] => 10 [Sambir] => 9 )
<?php $players = array("Sambir"=>"9","Rupal"=>"10","Aadarsh"=>"8"); arsort($players); print_r($players); ?> Output: Array( [Rupal] => 10 [Sambir] => 9 [Aadarsh] => 8 )
<?php $players = array("Sambir"=>"9","Rupal"=>"10","Aadarsh"=>"8"); krsort($players); print_r($players); ?> Output: Array( [Sambir] => 9 [Rupal] => 10 [Aadarsh] => 8 )
<?php $players = array("Sambir","Rupal","Aadarsh"); shuffle($players); print_r($players); ?> Output: /* Every time you refresh the value changes. */ Array( [0] => Aadarsh [1] => Sambir [2] => Rupal )