26 C
Jaipur
Wednesday, February 24, 2021

Use of ksort() function in PHP – Linux Hint

Must read

Many built-in functions exist in PHP to sort the array in different ways. ksort() function is one of them. This function is used to sort the array based on its key value, and it is mainly used to sort the associative array in ascending order based on key. How this sort function can be used in a PHP array is explained in this tutorial.

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

This function can take two arguments. The first argument is mandatory, which takes the array that will be sorted based on the key. The second argument is optional, which can be used to modify the behavior of the sort. Any of the following values can be used for the optional argument.

SORT_REGULAR or 0 : It is the default value and sorts the elements normally.
SORT_NUMERIC or 1 : It is used to sort the array based on numeric keys.
SORT_STRING or 2 : It is used to sort the array based on string keys.
SORT_LOCALE_STRING or 3 : It is used to sort the array based on string keys on the current locale.
SORT_NATURAL or 4 : It is used to sort the array based on string keys in a natural ordering.
SORT_FLAG_CASE or 5 : It is used to sort the array based on string keys in a case-sensitive manner.

Example 1: Sort array based on string keys (default)

The following example shows the use of ksort() without an optional argument.

First, create a PHP file with the following script. One dimensional associative array of four elements are declared in the script. Here, the ksort() function is used to sort the array based on the key values in ascending order. If no optional argument is used in the ksort() function, then it will sort the array normally. Next, the foreach loop is used to print the sorted array with keys and values.

<?php
//Declare an associative array
$clients = array(“c4089”=>“Mehrab Hossain”, “c1289”=>“Munir Chowdhury”, “c2390”=>“Meena Rahman”, “c1906”=>“Roksana Kamal”);

//Apply default ksort()
ksort($clients);

echo “<h2>The sorted array values are:</h2>”;

//Print the array values after sort
foreach ($clients as $key => $value) {
    echo $key = $value<br />”;
}
?>

Output:
The following output will appear after running the script from the server. The output shows that the key values of the array are sorted.

Example 2: Sort array based on numeric keys

The following example shows the way to sort the one-dimensional numeric array using the ksort() function.

First, create a PHP file with the following script. Here, an associative array of four elements is declared, where the key values of the array are numeric. 1 is used as the optional argument value of ksort() in the script that is used to sort an array based on numeric key values. Next, a foreach loop is used to print the sorted array.

<?php
//Declare an associative array
$items = array(89564=>“Monitor”, 98765=>“Mouse”, 34234=>“Printer”, 18979=>“Scanner”);

//Apply ksort() with optional argument value 1
ksort($items, 1);

echo “<h2>The sorted array values are:</h2>”;

//Print the array values after sort
foreach ($items as $key => $value) {
    echo $key = $value<br />”;
}
?>

Output:
The following output will appear after running the script from the server. The output shows the array keys and values after sorting the array based on numeric key values.

Example 3: Sort associative array on natural ordering

The following example shows the way to sort the array based on the key where the key will be sort on natural ordering. This means that if the key of the array starts with the character and ends with the number, then the sort will be done naturally.

First, create a PHP file with the following script. Here, an associative array of four elements is defined, and the key-value contains both characters and numbers. 4 is used in the second argument of ksort() for natural sorting. Next, a foreach loop is used to print the sorted array.

<?php
//Declare an associative array
$courses = array(‘CSE408’=>“Multimedia”, ‘MAT201’=>“Mathematics I”, ‘CSE204’=>“Algorithms”, ‘PHY101’=>“Physics I”);

//Apply ksort() with optional argument value 4
ksort($courses, 4);

echo “<h2>The sorted array values are:</h2>”;

//Print the array values after sort
foreach ($courses as $key => $value) {
    echo $key = $value<br />”;
}
?>

Output:
The following output will appear after running the script from the server. The output shows the array keys and values after sorting the array keys naturally.

Example 4: Sort associative array in a case-sensitive manner

The following example shows the way of sorting an associative array based on keys where the key values will be sorted in a case-sensitive manner.

First, create a PHP file with the following script. An associative array of five elements is declared in the script. The key values of the array contain both small letters and capital letters. Here, 5 is used as the value of the second argument value of ksort()for case-sensitive sort. Next, a foreach loop is used to print the sorted array.

<?php
//Declare an associative array
$foods = array(‘cake’=>$20, ‘Coke’=>$2, ‘burger’=>$5, ‘Pizza’=>$10, ‘donut’=>$2);

//Apply ksort() with optional argument value 5
ksort($foods, 5);

echo “<h2>The sorted array values are:</h2>”;

//Print the array values after sort
foreach ($foods as $key => $value) {
    echo $key = $value<br />”;
}
?>

Output:
The following output will appear after running the script from the server. The output shows the array keys and values after sorting the array keys in a case-sensitive manner. The key values of the array are cake, Coke, burger, Pizza, and a donut. The capital letter is smaller than the small letter based on the ASCII code. After the case-sensitive sort, the key values are Coke, Pizza, burger, cake, and a donut.

Conclusion

Different ways to sort the array based on keys using the ksort() function have been explained in this tutorial with the use of multiple examples. The key values are sorted based on the second argument value of this function. Hopefully, the readers will be able to sort the array based on keys using PHP after reading this tutorial.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article