In PHP, an inbuilt function ksort() is a function defined as a function that can sort the array especially an associated array in ascending order, and k in the function indicates key so ksort() is a function for sorting the array in ascending order and for descending order it will be krsort() function in PHP and hence the ksort() function returns the Boolean value if the sorting is done in ascending order according to keys or it will return false if this function fails. In general, sorting is defined as an arrangement of data or elements in an array form and in PHP to sort an array in ascending order according to the key is ksort() function.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
In this article, we will discuss the ksort() function in PHP. In this PHP programming language sorting is done using sort() but there are two ways of sorting such as according to values asort() for ascending and arsort() for descending order or according to keys we use ksort() for ascending order and krsort() for descending order. In general, the ksort() is a built-in function in PHP for sorting an associated array in ascending order according to keys but cannot sort in value and returns true if the array is sorted in proper ascending order based on keys and false if it fails.
In below we will see syntax and examples of ksort() function in PHP:
Syntax:
ksort(arr_to_sort, type_of_sort);
Parameters:
This ksort() function of PHP returns a Boolean value such as true if success and false if it fails.
Now let us see an example of how to declare the ksort() function in PHP:
Code:
<!DOCTYPE html> <html> <head> <title> Educba- ksort() in PHP </title> </head> <body> <?php echo"<h1>Demonstration of ksort() function on numeric values in PHP</h1>"; echo"<br>"; echo"<br>"; $arr = array("13" =>"Andrew", "12" =>"Zeva", "11" =>"Sam", "4" =>"Suchi", "5" =>"Tom", "6" =>"Harry", "4" =>"Tim", "8" =>"Carl", "7" =>"Ben", "10" =>"Nick", "1" =>"Ron", "2" =>"Peter", "3" =>"Emmanuel", "0" =>"Steve", ); ksort($arr); echo"Sorting of numeric values in ascending order"; echo"<br>"; foreach ($arr as $key => $val) { echo"<br>"; echo "[$key] = $val"; echo "<br>"; } ?> </body> </html>
Output:
In the above program, we can see that first, we have declared PHP code within php ?> . In the above code, we have first declared and defined the array using the array() function and have stored it in a variable known as “$arr”. In this array, we have defined a few names and we have assigned a number which here we take them as a key, and in this code, we are taking “key => value” format. Therefore after defining the array we are applying the ksort() function to this array “$arr” just by passing this array as an argument to the ksort() function. Then to display each element in the array we have to use the “for” loop. So after applying this ksort() function then we call the “for” loop where the elements are already sorted numerically as keys here are of numeric form and then we are printing for each key its respective value in ascending order. This output can be seen in the above screenshot.
Now let us see an example if keys are string using ksort() function in PHP.
Code:
<html> <head> <title> Educba- ksort() in PHP </title> </head> <body> <?php echo "<h1>Demonstration of ksort() with strings in PHP</h1> \n"; echo "<br>"; $arr1 = array("Educba" => "1", "Google" => "2", "Facebook" => "3", "Alibaba" => "4", "Samsung" => "5", "Zen" => "6", ); ksort($arr1); echo "Sorting of string in ascending order"; echo "<br>"; foreach ($arr1 as $key => $val) { echo "<br>"; echo "[$key] = $val"; echo "<br>"; } ?> </body> </html>
Output:
In the above program, we can see we have written the PHP code within Php ?>. Firstly defined and declared array name as $arr1 and the array is defined using array() function. In this array, we have used a few company names as keys and the numbers assigned to them as values. Then we have applied the ksort() function to this array $arr1 which is done by passing the $arr1 as an argument to the method ksort() in PHP. Therefore this function will then sort the array alphabetically as the key defined here is in string format and hence the array is sorted in ascending order of alphabets and not of numbers and to display each element in the array we need to use “for” loop where we are displaying each key with its values in the alphabetic order as keys are in string format. The output can be seen in the above screenshot.
In this article, we saw only the ascending order arrangement or sorting of the given array using the ksort() function. But in PHP there are other functions where we can sort array elements based on values in both ascending and descending order such as asort() & arsort() respectively, and also there is a function for sorting the array based on a key in descending order such as krsort(). In PHP, sorting can be done using the sort() function as we do in other programming languages but in PHP it provides separate functions for each order as well as for each key and value based arrangement.
In this article, we conclude that the ksort() function in PHP is defined for arranging or sorting the array elements passed to this function as an argument in ascending order based on keys defined to each element. In this article, we saw there are other functions for descending order as well as for sorting based on values such as krsort(), asort(), arsort() respectively. In this article, we saw a few examples of how this ksort() function works on numeric keys and keys in string format wherein numeric keys sorted the array based on keys in ascending order, and in the case of string as keys, it sorted the elements in ascending alphabetic order.
The above is the detailed content of PHP ksort. For more information, please follow other related articles on the PHP Chinese website!