PHP ksort() Function

PHP ksort() Function

The ksort() function in PHP sorts an array by key while maintaining the keys.

Syntax

ksort(array & $array[,int $sort_flags = SORT_REGULAR] )

Parameter

array(required)- This parameter represents the input array.

sort_flags(optional)- This parameter is used to modify the behavior of the sort. The various sorting type flags are as follows:

  • SORT_REGULAR – It is used to compare items normally without changing types.
  • SORT_NUMERIC – This flag compares items numerically
  • SORT_STRING – It compares the items as strings
  • SORT_LOCALE_STRING – This flag compares the given items as strings, based on the current locale.
  • SORT_NATURAL – It is used to compare the items as strings using "natural ordering".
  • SORT_FLAG_CASE – This flag can be combined with SORT_STRING or SORT_NATURAL to sort strings case-insensitively.

Return

This parameter returns a Boolean value TRUE on success or FALSE on failure.

Example 1

"Reema", "a"=>"Varun", "b"=>"Mishra", "c"=>"Radhika");
echo("Initialized Array: ");
print_r($array);
//soring the keys if the array
ksort($array);
echo("\nSorted Array: \n");
print_r($array);//the print order will be a,b,c,d
?> 

Output

Initialized Array: Array
(
[d] => Reema
[a] => Varun
[b] => Mishra
[c] => Radhika
)
Sorted Array: 
Array
(
[a] => Varun
[b] => Mishra
[c] => Radhika
[d] => Reema
) 

Example 2

 array(1 => "mango", 2 => "banana", 2 => "apple"),
"numbers" => array(1, 2, 3, 4, 5, 6),
"values" => array("first", 5 => "second", "third")
);
//sorting the keys of the array
ksort($array);
//printing the sorted key array
print_r($array);
?> 

Output

Array
(
    [fruits] => Array
        (
            [1] => mango
            [2] => apple
        )
    [numbers] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
        )
    [values] => Array
        (
            [0] => first
            [5] => second
            [6] => third
        )

Example 3

 $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}
//Sorting the keys of the array 
ksort($fruits);
echo("\n\nAfter sorting the keys of the array...\n Reversed Array: \n");
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}
?> 

Output

Original Array: 
fruits[0] = lemon
fruits[1] = orange
fruits[2] = banana
fruits[3] = apple
After sorting the keys of the array...
 Reversed Array: 
fruits[0] = lemon
fruits[1] = orange
fruits[2] = banana
fruits[3] = apple 

Example 4

"Reema", "Age"=>23,"Country"=>"India",1=>"Love");
//original array
echo("Original array: \n");
print_r($array);
//sorting the keys of the array in reverse order
ksort($array ); 
//printing the sorted array
echo("\nSorted array without the second parameter: \n");
print_r($array);
//sorting array with second parameter as SORT_STRING with keys in reverse order
ksort($array,SORT_STRING );
//printing the sorted array
echo("\nSorted array with the second parameter: \n");
print_r($array);
?> 

Output

Original array: 
Array
(
    [Name] => Reema
    [Age] => 23
    [Country] => India
    [1] => Love
)
Sorted array without the second parameter: 
Array
(
    [Age] => 23
    [Country] => India
    [Name] => Reema 
    [1] => Love
)
Sorted array with the second parameter: 
Array
(
    [1] => Love
    [Age] => 23
    [Country] => India
    [Name] => Reema
)