Tutorials Point


  Learning PHP
  Advanced PHP
  PHP Function Reference
  PHP Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     Discussion Forums     About TP  

PHP Function ksort()


previous

Advertisements

Syntax

ksort ( $array, $sort_flag );

Definition and Usage

The ksort() function sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays.

Paramters

ParameterDescription
arrayRequired. Specifies an array
sort_flag 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

Return Value

This function returns TRUE on success, or FALSE on failure.

Example

Try out following example:

<?php
$transport = array( a=>'foot', b=>'bike', c=>'car', d=>'plane');
ksort($transport);
print_r($transport);
?> 

This will produce following result:

Array ( [a] => foot [b] => bike [c] => car [d] => plane ) 

previous Printer Friendly

Advertisements


  

Advertisements