PHP krsort() Function Last Updated : 20 Jun, 2023 Comments Improve Suggest changes Like Article Like Report The krsort() function is an inbuilt function in PHP which is used to sort an array by key in reverse order according to its index values. It sorts in a way that relation between indices and values is maintained. Syntax: bool krsort( $array, $sorting_type ) Parameters: This function accepts two parameters as mentioned above and described below: $array: This parameter specifies the array which to be sort. It is a mandatory parameter. $sorting_type: This is an optional parameter. There are different sorting types which are discussed below: SORT_REGULAR: The value of $sorting_type is SORT_REGULAR then items are compare normally. SORT_NUMERIC: The value of $sorting_type is SORT_NUMERIC then items are compares numerically. SORT_STRING: The value of $sorting_type is SORT_STRING then items are compares as string. SORT_LOCALE_STRING: The value of $sorting_type is SORT_STRING then items are compares as string, based on current locale. Return Value: This function returns True on success or False on failure. Below programs illustrate the krsort() function in PHP. Program 1: php <?php // PHP program to illustrate // krsort()function // Input different array elements $arr = array("0" =>"Technology", "1" =>"Machine", "2" =>"GeeksforGeeks", "3" =>"Graphics", "4" =>"Videos", "5" =>"Report", "6" =>"Article", "7" =>"Placement", "8" =>"Contribute", "9" =>"Reset", "10" =>"Copy", ); // Implementation of krsort() krsort($arr); // for-Loop for displaying result foreach ($arr as $key => $val) { echo "[$key] = $val"; echo"\n"; } ?> Output: [10] = Copy [9] = Reset [8] = Contribute [7] = Placement [6] = Article [5] = Report [4] = Videos [3] = Graphics [2] = GeeksforGeeks [1] = Machine [0] = Technology Program 2: php <?php // PHP program to illustrate // krsort function // Input different array elements $arr = array("a" => 11, "b" => 22, "d" => 33, "n" => 44, "o" => 55, "p" => 66, "r" => 77, "s" => 2, "q" => -11, "t" => 3, "u" => 1000, "z" => 1, ); // Implementation of krsort krsort($arr); // for-Loop for displaying result foreach ($arr as $key => $val) { echo "[$key] = $val"; echo"\n"; } ?> Output: [z] = 1 [u] = 1000 [t] = 3 [s] = 2 [r] = 77 [q] = -11 [p] = 66 [o] = 55 [n] = 44 [d] = 33 [b] = 22 [a] = 11 Related Articles: sort() Function asort() Function arsort() Function Reference: http://php.net/manual/en/function.krsort.php Comment More infoAdvertise with us Next Article PHP krsort() Function jit_t Follow Improve Article Tags : PHP PHP-array Similar Reads PHP array_chunk() Function The array_chunk() function is an inbuilt function in PHP which is used to split an array into parts or chunks of given size depending upon the parameters passed to the function. The last chunk may contain fewer elements than the desired size of the chunk. Syntaxarray array_chunk( $array, $size, $pre 2 min read PHP array_combine() Function The array_combine() function is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values. That is all elements of one array will be the keys of new array and all elements of the second array will be the values of t 2 min read PHP array_count_values() Function The array_count_values() is an inbuilt function in PHP which is used to count all the values inside an array. In other words we can say that array_count_values() function is used to calculate the frequency of all of the elements of an array. Syntax: array array_count_values( $array ) Parameters: Thi 2 min read PHP array_diff_assoc() Function This inbuilt function of PHP is used to get the difference between one or more arrays. This function compares both the keys and values between one or more arrays and returns the difference between them. So, the function generally compares two arrays according to there keys and values and returns the 2 min read PHP array_diff_key() Function This inbuilt function of PHP is used to get the difference between one or more arrays. This function compares the keys between one or more arrays and returns the difference between them. So, the function generally compares two arrays according to their keys and returns the elements that are present 2 min read PHP array_diff_uassoc() Function The array_diff_uassoc() function is a built-in function in PHP and is used to get the difference between one or more arrays using an user-defined function to compare the keys. This function compares both the keys and values between one or more arrays to and returns the elements from the first array 4 min read PHP array_diff_ukey() Function The array_diff_ukey() function is an inbuilt function in PHP. It is used to compares the key two or more arrays using a user-defined function, and return an array, which is array1 and its not present any others array2, array3 or more... Syntax: array_diff_ukey($array1, $array2, $array3..., arr_diffu 4 min read PHP array_diff() function The array_diff() is an inbuilt function in PHP ans is used to calculate the difference between two or more arrays. This function computes difference according to the values of the elements, between one or more array and return differences in the form of a new array. This function basically returns a 2 min read PHP array_fill_keys() Function The array_fill_keys() function is a builtin function in PHP and is used to create a new array filled with the given keys and value provided as an array to the function. Syntax: array array_fill_keys ( $keys, $value ) Parameters: This function accepts two parameters, keys and their values to be prese 3 min read PHP array_fill() function The array_fill() is an inbuilt-function in PHP and is used to fill an array with values. This function basically creates an user-defined array with a given pre-filled value. Syntax: array_fill($start_index, $number_elements, $values) Parameter: The array_fill() function takes three parameters and ar 2 min read Like