SQL PHP HTML ASP JavaScript articles and free scripts to download
 

krsort: Sorting Key by reverse order

We have seen how the ksort function sorts the array based on the keys of the element. By using krsort we can just reverse the order of the keys. Here also we have retained the data key association after the krsort function.




Let us try with some examples.







<?
$input = array ("b" => "Pinaple", "d" =>"Orange", "a" =>"Banana", "c" =>"Mango", "f" =>"Apple","e" =>"Strawberry");
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
krsort($input);
echo "<br>-----After krsort---------<br>";
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
?>
The output of the above code is here

b -> Pinaple
d -> Orange
a -> Banana
c -> Mango
f -> Apple
e -> Strawberry

-----After krsort---------
f -> Apple
e -> Strawberry
d -> Orange
c -> Mango
b -> Pinaple
a -> Banana

Discuss this tutorial at forum


Further readings
 PHP array
array : Creating an Array
array_diff Difference of two arrays
array_count_values counting the frequency of values inside an array
count : sizeof Array Size or length
array_push : Adding element to an Array
array_sum : Array Sum of all elements
Displaying the elements of an array
Array checkbox
in_array : IN Array to search for elements inside an array
array_rand : Random elements of Array
array_unique : Array Unique values
Breaking of strings to create array using split command
unset : Deleting elements of an array by using its key
sort: Simple sorting of PHP array
rsort: Reverse sorting of PHP array
asort: Maintaining index of the elements after sorting
arsort: Sorting in reverse order for a PHP array
ksort: Array key Sorting in PHP
krsort: Array reverse key Sorting in PHP
usort: Sorting array by using user defined function














 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
Array Functions
array_diff
array_count_values
array_size
array_push
array_sum
in_array
array_rand
array_unique
Split String
unset (delete)
sort
rsort
asort
arsort
ksort
krsort
usort
Displaying elements
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
Date & Time
Array
String Functions
Math Functions
Form Handling
File Handling
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.