Sort an associative array by its keys in ascending order.
<?php $age=array(“Peter”=>“35”,“Ben”=>“37”,“Joe”=>“43”); ksort($age); ?> |
The ksort() function sorts an associative array in ascending order based on its keys.
Tip: Use the krsort() function to sort an associative array in descending order by key.
Tip: Use the asort() function to sort an associative array in ascending order by value.
ksort(array, sorttype) |
Parameter |
Description |
array |
Required. Specifies the array to be sorted. |
sorttype |
Optional. Specifies how to compare the array elements/items. Possible values are:
|
Return Value: |
Returns TRUE on success, or FALSE on failure. |
PHP Version: |
4+ |