Easy Tutorial
❮ Filter Sanitize String Func Date Sunrise ❯

PHP ksort() Function

Complete PHP Array Reference Manual

Example

Sort an associative array by key in ascending order:


Definition and Usage

The ksort() function sorts an associative array by key in ascending order.

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

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

Syntax

Parameter Description
array Required. Specifies the array to sort.
sortingtype Optional. Specifies how to arrange the array elements/items. Possible values: 0 = SORT_REGULAR - Default. Sort items normally (Standard ASCII, do not change types).<br> 1 = SORT_NUMERIC - Treat each item as a number.<br> 2 = SORT_STRING - Treat each item as a string.<br> 3 = SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed with setlocale()).<br> 4 = SORT_NATURAL - Treat each item as a string, using natural ordering like natsort().<br> 5 = SORT_FLAG_CASE - Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively.

Technical Details

Return Value: Returns TRUE on success, FALSE on failure.
PHP Version: 4+
--- ---
❮ Filter Sanitize String Func Date Sunrise ❯