Thursday, 25 September 2014

asort in PHP

asort() function is utilized to sort an input array by the values. The keys relating to the values are kept as same.
The PHP asort() function returns TRUE on success, or FALSE on failure.

Syntax:

asort(array,sorttype)
 
Parameters Description: 
array : Required. Specifying an array
sorttype : Optional. Specifies how to sort the array values.
     Possible values:
  • SORT_REGULAR - Default. Treat values as they are (don't change types)
  • SORT_NUMERIC - Treat values numerically
  • SORT_STRING - Treat values as strings
  • SORT_LOCALE_STRING - Treat values as strings, based on local settings

Example:

<?php  
$daysarr1 = array( "a"=>"Sunday","b"=>"Monday","c" =>"Tuesday" );
asort($daysarr1);
print_r($daysarr1);
?>

Output will be:

Array (
         [b] => Monday 
         [a] => Sunday
         [c] => Tuesday
       )

0 comments:

Post a Comment