Thursday, 25 September 2014

ksort in PHP

PHP ksort() function is utilized to sort an array by key. The values keep their unique keys.

PHP ksort() function returns TRUE for success, or FALSE on failure.

Syntax:

ksort(array,sorttype)
 
Parameters Description: 
array : Required. Specifies the array to use.
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" );
ksort($daysarr);
print_r($daysarr);
?>

Output will be:

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

0 comments:

Post a Comment