Behrooz Valikhani
Behrooz Valikhani

Reputation: 85

Sort the values of your array and reverse it

I have an array like this:

The output is as below:

enter image description here

I want to sort the image output from right to left
Does anyone know the solution?

Thanks

Upvotes: 0

Views: 39

Answers (1)

Sebastian Brosch
Sebastian Brosch

Reputation: 43594

You can use sort to sort the array in ascending order (0 to 30). To reverse all the elements of the sorted array you can use array_reverse:

$arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];
sort($arr);
$arr = array_reverse($arr);

demo: https://ideone.com/8Sa3sh

You can also use rsort to sort directly in descending order:

$arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];
rsort($arr);

demo: https://ideone.com/pi33zK

Upvotes: 1

Related Questions