ksort
Sort arrays by key names
ksort()
function sorts the associative arrays according to the key names.
Tip: Please use the krsort()
function to sort the associative arrays in descending order by key names.
Tip: Please use the asort()
function to sort the associative arrays in ascending order by key values.
Sort the associative array by key name:
<?php $age = array ( "Bill" => "60" , "Steve" => "56" , "mark" => "31" ) ; ksort ( $age ) ; ?>
Try it yourself
ksort ( array , sortingtype ) ;
parameter | describe |
---|---|
array | Required. Specifies the array to be sorted. |
sortingtype |
Optional. Specifies how to arrange elements/items of an array. Possible values:
|
ksort()
function sorts the array by key name and retains the original key for the array value.
The optional second parameter contains the additional sort flag.
If successful, return TRUE, otherwise return FALSE.