Home >Backend Development >PHP Tutorial >In PHP, the function of ksort() function is to sort the array in ascending order by key name.
ksort() function sorts an array in ascending order by key. Returns TRUE on success and FALSE on failure.
ksort(arr, flag)
arr - The array to sort.
Flags -
0 = SORT_REGULAR - Default. Compare items normally. Don't change the type.
1 = SORT_NUMERIC - compares items numerically
2 = SORT_STRING - compares items numerically Compare as strings
3 = SORT_LOCALE_STRING - Compare items as strings based on the current locale
4 = SORT_NATURAL - Compare items as strings using natural ordering.
ksort() function returns TRUE when successful and FALSE when failed.
The following is an example
Live demonstration
<?php $product = array("headphone"=>"10", "mouse"=>"24", "pendrive"=>"13"); ksort($product); foreach ($product as $key => $val) { echo "$key = $val</p><p>"; } ?>
headphone = 10 mouse = 24 pendrive = 13
The above is the detailed content of In PHP, the function of ksort() function is to sort the array in ascending order by key name.. For more information, please follow other related articles on the PHP Chinese website!