In PHP, the ksort function is mainly used in associative array.
The "ksort()" function is used for sorting an array elements by the key and keep the original values.
php developer Syntax :
php 7 Sample Code :
php program Code Explanation :
In this statement “$var1” variable is defining an associative array using array function with its keys such as “Lemon, Apple and Grape” and the values as “34,50, and 25”.
The var1 value will be sorted using the ksort function where the array keys are sorted in ascending order.
In this statement we use the for each loop that will be used for looping process in an associative array. The first variable of the for loop defines the array name “$var1” then “$x” defines the key, and “$x_value” defines the value.
echo statement is used for print the value sorted key and their values and the “.” Dot symbol is used for concatenation of the string in php.
php coding Sample Output :
Output of the keys (Lemon, Apple and Grapes) are sorted in ascending order (Apple, Lemon and Grapes) by the key and keeping the original values (50, 25 & 34) as it is.