Is there an option in PHP to sort an array by key value descending?
I am aware that you can sort key values with ksort
The arsort() function sorts an associative array in descending order, according to the value. Tip: Use the asort() function to sort an associative array in ascending order, according to the value. Tip: Use the krsort() function to sort an associative array in descending order, according to the key.
We can use . sort((a,b)=>a-b) to sort an array of numbers in ascending numerical order or . sort((a,b)=>b-a) for descending order.
Answer: Use the PHP ksort() and krsort() function The PHP ksort() and krsort() functions can be used for sorting an array by key.
Loop through the array and select an element. Inner loop will be used to compare selected element from outer loop with rest of the elements of array. If any element is greater than the selected element then swap the values. Continue this process till entire list is sorted in descending order.
Yes, the krsort
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
krsort($fruits);
I believe krsort() is what you're looking for.
http://www.php.net/manual/en/function.krsort.php krsort?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With