PHP ksort() Function

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

Tip: Use the krsort() function to sort an associative array in descending order, according to the key.

Tip: Use the asort() function to sort an associative array in ascending order, according to the value.

ParameterDescription
arrayRequired. Specifies the array to sort
sortingtypeOptional. Specifies how to compare the array elements/items. Possible values:

Syntax

ksort(array,sortingtype);

Example

$age=array("Samuel"=>"35","James"=>"37","Cooper"=>"43");
ksort($age);
print_r($age);

Output

Array ( [Cooper] => 43 [James] => 37 [Samuel] => 35 ) 

Leave a Reply

Your email address will not be published. Required fields are marked *