Javatpoint Logo
Javatpoint Logo

PHP ksort() function

The PHP ksort() sorts an associative array in ascending order, according to the key. It is mainly used for an associative array. This function was introduced in PHP 4.0.

Syntax

Parameter

Parameter Description Is compulsory
array Specifies the array to use. compulsory
sort_flag It specifies how to sort the array values. Possible values -
SORT_REGULAR - Default. Treat values as they are (don't change types)
SORT_NUMERIC - Treat values numerically
SORT_STRING - Treat values as strings
SORT_LOCALE_STRING - Treat values as strings, based on local settings.
Optional

Returns

The ksort() function returns true on success and false on failure.

Example 1

Output:

Array
(
    [ganguly] => 46
    [sachin] => 45
    [virat] => 29
)

Example 2

Output:

Array
(
    [a] => cricket
    [b] => hockey
    [c] => football
    [d] => badminton
)

Example 3

Output:

Array
(
    [china] =>beijing
    [france] =>paris
    [india] =>newdelhi
    [russia] =>moscow
    [united kingdom] =>london
)

Example 4

Output:

Array
(
    [z] => ruby
    [y] => java
    [x] => html
)





Help Others, Please Share

facebook twitter pinterest