How to sort an array in descending order in PHP?

To sort an array in descending order in PHP, you can use the rsort() or arsort() functions. Here's an example:

$array = [5, 2, 8, 1, 9]; // Option 1: rsort() rsort($array); print_r($array); // Output: Array ( [0] => 9 [1] => 8 [2] => 5 [3] => 2 [4] => 1 ) // Option 2: arsort() arsort($array); print_r($array); // Output: Array ( [1] => 8 [0] => 9 [2] => 5 [3] => 2 [4] => 1 )

In the rsort() function, the array elements are sorted in descending order based on their values. However, the keys are not preserved.

In the arsort() function, the array elements are sorted in descending order based on their values, and the keys are preserved.

You can choose the appropriate function based on whether you need to preserve the keys or not.