To sort an array in descending order in PHP, you can use the `rsort` function for arrays of numbers and the `arsort` function for arrays of strings.
In this example,
$numbers
.rsort
function to sort the array of numbers in descending order.<?php
$numbers = [5, 2, 8, 1, 9];
rsort($numbers);
print_r($numbers);
?>
Array ( [0] => 9 [1] => 8 [2] => 5 [3] => 2 [4] => 1 )
In this example,
$words
.arsort
function to sort the array of strings in descending order.<?php
$words = ["apple", "banana", "cherry", "date"];
arsort($words);
print_r($words);
?>
Array ( [3] => date [2] => cherry [1] => banana [0] => apple )
In this tutorial, we learned How to Sort an Array in Descending Order in PHP language with well detailed examples.