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
) 

Related Topics

PHP array_map() Function

PHP array_map() Function The array_map() function  in PHP is used to return an array containing the results of applying the user-defined callback function over the elements of one or more arrays. Syntax array_map ( callable $callback , array $array1 [, array $... ] )  Parameter callable(required)- This parameter...

1 minute read.

PHP array_combine() Function

PHP array_combine() Function The array_combine() function in PHP is used to create an array by combining two arrays i.e., one array for keys and another array for its values. Syntax array_combine ( array $keys , array $values ) Parameter keys(required)- This parameter represents an array which acts...

1 minute read.

PHP str_replace() Function

PHP str_replace() Function The str_replace() function in PHP replaces all occurrences of the search string with the specified replacement string. This function is case-sensitive. Syntax str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter signifies the value to search. replace(required)-...

1 minute read.

PHP nl2br() Function

PHP nl2br() Function The nl2br() function in PHP inserts HTML line breaks before all newlines in a string. Syntax nl2br ( string $string [, bool $is_xhtml] ) Parameter sting(required)- This parameter specifies the input string. is_xhtml(optional)- This parameter states whether to use XHTML compatible line breaks...

1 minute read.

PHP array_slice() Function

PHP array_slice() Function The array_slice() function in PHP is used to extract a slice of the array. It returns the sequence of elements from the array as specified by the offset and length parameters. Syntax array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = FALSE ]] ) Parameter array(required)- This array represents the...

2 minutes read.

PHP implode() Function

PHP implode() Function The implode() function in PHP joins the specified array elements with a string. Syntax implode ( string $glue, array $pieces ) Parameter glue(optional)- It specifies what to put between the array elements. pieces(required)- This parameter represents...

1 minute read.

PHP Operator

PHP Operator is used to perform operations on variable and values. Following are the types of Operator: PHP Arithmetic Operator PHP Arithmetic operator is used for arithmetical operations, such as addition, subtraction, multiplication...

2 minutes read.

PHP gettype() Function

PHP gettype() Function The gettype () function in PHP gets the type of the specified variable. Syntax gettype ( mixed $var ) Parameter var(required)- This parameter represents the variable being type checked. Return This function returns the type of the PHP variable specified by “var”...

1 minute read.

PHP strlen() Function

PHP strlen() Function The strlen() function in PHP is used to return the length of the specified string. Syntax strlen ( string $string ) Parameter string(required)- This parameter represents the string which is measured for length. Return This function returns the length of the string on success or returns 0 if the specified...

1 minute read.

PHP is_double() Function

The PHP is_double() function in PHP is used to  find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter var is...

1 minute read.

PHP array_splice() Function

PHP array_splice() Function The array_splice() function in PHP is used to remove a portion of the array and replaces them with the elements of the replacement array. Syntax array_splice (array &$input, int $offset [, int $length = count($input) [, mixed $replacement = array() ]] ) Parameter input(required)- This array represents the...

2 minutes read.

Multiple-Inheritance in PHP

Multiple-Inheritance in PHP Multiple-Inheritance is the resources of the Object Oriented Programming Languages in which child class or subclass can inherit the resources of the multiple parent classes or superclasses. PHP does...

3 minutes read.

PHP array_shift() Function

PHP array_shift() Function The array_shift() in PHP shifts the first value of the array off. This function returns the same value after shortening the array by one element and moving everything. Syntax array_shift ( array &$array )  Parameter array(required)- This parameter represents the input array Return This function returns...

1 minute read.

WordPress Plugin

WordPress Plugin is a process used to add or extend the functionality which can be enabled or disabled, and it does not interfere with the software and its code. WordPress is lightweight and smooth...

2 minutes read.

PHP array_product() Function

PHP array_product() Function The array_product() function in PHP calculates the product of values in an array. Syntax array_product ( array $array ) Parameter array(required)- This parameter represents the input array. Return This function returns the product of the array elements as an integer or float....

1 minute read.

PHP quoted_printable_decode() Function

PHP quoted_printable_decode() Function The quote_printable_decode() function of PHP converts a quoted-printable string to an 8 bit string. It works similar to imap_qprint(), except this one does not require the IMAP module to work. Syntax quoted_printable_decode ( string $str ) Parameter str(required)- This parameter represents the...

1 minute read.

PHP fprintf() Function

PHP fprintf() Function The fprintf() function in PHP outputs a formatted string. This function operates as printf() but accepts an array of arguments, rather than a variable number of arguments. Syntax vprintf ( string $format , array $args ) Parameter format(required)- This parameter specifies the string and how to...

4 minutes read.

PHP setlocale() Function

PHP setlocale() Function The setlocale() function in PHP sets locale information(language, monetary, time and, other information specific for a geographical area.) Syntax setlocale ( int $category , array $locale ) Parameter category(required)- This parameter specified a named constant specifying the category of the functions affected...

1 minute read.

PHP settype() Function

PHP settype() Function The settype() function in PHP is used to set the type of variable var to type. Syntax settype ( mixed &$var , string $type ) Parameter var(required)- This parameter represents the variable being converted. type(required)- This parameter represents the type of variable that is needed.  The possible values of type are...

1 minute read.

PHP ucfirst() Function

PHP ucfirst() Function The ucfirst() function in PHP converts the first character of the specified string to uppercase. Syntax ucfirst ( string $str ) Parameter str(required)- This parameter represents the input string. Return This function returns a string with the first character of the specified string capitalized if that...

1 minute read.