PHP Function Ksort
PHP provides a number of built-in functions for sorting array elements in different ways. Some of these are used for sorting array elements alphabetically, while others are used for sorting array elements numerically or in ascending or descending order. The ksort function is used for sorting associative array elements by the key while keeping the original values.
The ksort() function takes in two parameters. The first is mandatory and specifies the array that needs to be sorted by the keys. The second is optional and can be used to modify the behavior of ksort. For example, if the second parameter is SORT_REGULAR then items will be compared normally and won't change their types. However, if the second parameter is SORT_LOCALE_STRING then the items will be compared as strings based on the current locale.
For example, the below program shows how to use ksort to sort an associative array with its keys such as "Lemon, Apple and Grape" and its values such as "34,50 and 25". In this way, the associative array is sorted in ascending order by the key while keeping the values.
The ksort() function also allows you to sort the array in a case-sensitive manner. In this way, capital letters will be placed before the small letters. This is a different behavior from the other array sort functions in PHP which place numeric keys prior to alphabetical keys. This is a bug fix and will be changed in future versions of PHP to make it consistent with the other array sort functions.