PHP ksort()Functions
Examples
Sort an associative array in ascending order by key:
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
ksort($age);
?>
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
ksort($age);
?>
Run Example »
Definition and Usage
The ksort() function sorts an associative array in ascending order by key.
Tip:Use thekrsort()function to sort an associative array in descending order by key.
Tip:Use theasort()function to sort an associative array in ascending order by value.
Syntax
ksort(array,sortingtype);
| Parameters | Description |
|---|---|
| array | Required. Specifies the array to sort. |
| sortingtype | Optional. Specifies how to sort the array's elements/items. Possible values:
|
Technical Details
| Return Value: | Returns TRUE on success, or FALSE on failure. |
|---|---|
| PHP Version: | 4+ |
Complete PHP Array Reference Other Extensions