ksort()
Sort an array by its keys from lowest to highest as asort() does with values. Also like asort(), ksort() keeps the associated keys. Only true or false is returned (success or failure). This function alters the input array itself.
To sort from highest to lowest, see krsort() which works the same as this function.
Optional flags can be used as the second parameter. SORT_REGULAR is the default.
| SORT_REGULAR | Compare items normally (don’t change types) |
| SORT_NUMERIC | Compare items numerically |
| SORT_STRING | Compare items as strings |
| SORT_LOCALE_STRING | Compare items as strings, based on the current locale. |
If the keys are of mixed types, you may not get the result you expect in some cases.
$arr = array('test' => 'eggplant', 'banana', 'sample' => 'apple', 17 => 'dandelion', 'carrot'); ksort($arr); /* array ( 0 => 'banana', 'sample' => 'apple', 'test' => 'eggplant', 17 => 'dandelion', 18 => 'carrot' ) */ $arr = array('test' => 'eggplant', 'banana', 'sample' => 'apple', 17 => 'dandelion', 'carrot'); ksort($arr, SORT_STRING); /* array ( 0 => 'banana', 17 => 'dandelion', 18 => 'carrot', 'sample' => 'apple', 'test' => 'eggplant' ) */
See the manual entry for ksort()
