Thursday, 25 September 2014

sort in PHP

PHP sort() function is used to sort an array by values.
Function returns TRUE on success, or FALSE on failure.

Syntax:

sort(array,sorttype)
array : Required. Specifies the array to sort
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
$daysarr = array( "a"=>"Sunday", "b"=>"Monday", "c"=>"Tuesday" );
sort($daysarr);
print_r($daysarr);
?>

O/P:

Array (
        [0] => Monday 
        [1] => Sunday
        [2] => Tuesday
       )

0 comments:

Post a Comment