PHP Program to Sort an Array Elements in Descending Order
Last Updated :
11 Jul, 2024
Given an array containing some elements, the task is to sort the array elements in descending order in PHP. Sorting elements of an array is a common operation in programming, and PHP provides several methods to accomplish this task.
Using rsort() Function
The rsort()
function is a built-in PHP function specifically designed for sorting an array in descending order.
PHP
<?php
$arr = [5, 2, 8, 1, 3];
// Sort the array in descending order
rsort($arr);
// Display descending order sorted array
print_r($arr);
?>
OutputArray
(
[0] => 8
[1] => 5
[2] => 3
[3] => 2
[4] => 1
)
Using array_reverse() with sort() Functions
Another approach is to use the combination of array_reverse() and sort() functions to achieve the descending order sorting.
PHP
<?php
$arr = [5, 2, 8, 1, 3];
// Sort the array in ascending order
sort($arr);
// Reverse the array to get
//descending order
$arr = array_reverse($arr);
// Display the sorted array
// (Descending order)
print_r($arr);
?>
OutputArray
(
[0] => 8
[1] => 5
[2] => 3
[3] => 2
[4] => 1
)
Using usort() with Custom Comparison Function
The usort() function allows you to sort an array using a custom comparison function, providing more flexibility in sorting criteria.
PHP
<?php
$arr = [5, 2, 8, 1, 3];
// Sort the array in descending order
usort($arr, function ($a, $b) {
return $b - $a;
});
// Display the sorted array
// (descending order)
print_r($arr);
?>
OutputArray
(
[0] => 8
[1] => 5
[2] => 3
[3] => 2
[4] => 1
)
Using array_multisort()
array_multisort() sorts arrays and can handle multiple arrays simultaneously. To sort a single array in descending order
Example:
PHP
<?php
$array = [5, 2, 9, 1, 5, 6];
array_multisort($array, SORT_DESC);
print_r($array);
// Output will be visible in the console
?>
OutputArray
(
[0] => 9
[1] => 6
[2] => 5
[3] => 5
[4] => 2
[5] => 1
)
Using arsort() Function
The arsort() function is another built-in PHP function specifically designed for sorting an associative array in descending order according to its values while maintaining key association.
Example:
PHP
<?php
$arr = [5, 2, 8, 1, 3];
// Sort the array in descending order
arsort($arr);
// Display the sorted array
print_r($arr);
?>
de
?>
OutputArray
(
[2] => 8
[0] => 5
[4] => 3
[1] => 2
[3] => 1
)
de
?>
Using uksort() Function
The uksort() function sorts an associative array by keys using a user-defined comparison function, allowing customization for descending order sorting based on keys.
Example:
PHP
<?php
// Example associative array
$array = ['c' => 3, 'a' => 1, 'b' => 2];
// Sort array by keys in descending order using uksort()
uksort($array, function($a, $b) {
return strcmp($b, $a);
});
print_r($array); // Output: ['c' => 3, 'b' => 2, 'a' => 1]
?>
OutputArray
(
[c] => 3
[b] => 2
[a] => 1
)
Similar Reads
How to sort an array in descending order in Ruby? In this article, we will discuss how to sort an array in descending order in ruby. We can sort an array in descending order through different methods ranging from using sort the method with a block to using the reverse method after sorting in ascending order Table of Content Sort an array in descend
3 min read
How to get elements in reverse order of an array in PHP ? An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements
4 min read
How to Check an Array is Sorted or Not in PHP? Given an array, the task is to check whether the given array is sorted or not. Arrays are often used to store and manipulate data. One common task is to check if an array is sorted in ascending or descending order. This article will explore different approaches to determine whether an array is sorte
3 min read
PHP program to find the maximum and the minimum in array Finding the maximum and minimum in an array involves determining the largest and smallest values within a given set of numbers. This task is crucial for analyzing data, identifying trends, or filtering outliers. Various methods, from simple traversal to optimized algorithms, can achieve this.Example
7 min read
Sort an array of dates in PHP We are given an array of multiple dates in (Y-m-d) format. We have to write a program in PHP to sort all the dates in the array in decreasing order. Examples : Input : array("2018-06-04", "2014-06-08", "2018-06-05") Output : 2018-06-05 2018-06-04 2014-06-08 Input : array("2016-09-12", "2009-09-08",
2 min read
How to Switch the First Element of an Arrays Sub Array in PHP? Given a 2D array where each element is an array itself, your task is to switch the first element of each sub-array with the first element of the last sub-array using PHP.Example:Input: num = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']];Output: [ ['g', 'b', 'c'], ['d', 'e', 'f'], ['a', 'h', '
2 min read