PHP: Sort array numerically descending

20,241

Solution 1

Use rsort().

Solution 2

rsort( $outcomes_array )

Note, it is not

$outcomes_array = rsort( $outcomes_array );

Solution 3

rsort( $outcomes_array );
print_r( $outcomes_array );

Solution 4

  • rsort is just for a numeric array
  • arsort is for an array with keys

Solution 5

As the default is SORT_REGULAR - compare items normally (don't change types) So the code should be:

$outcomes_array = array(1,4,2,3,5);
rsort( $outcomes_array, SORT_NUMERIC );//SORT_NUMERIC - compare items numerically
print_r( $outcomes_array );
Share:
20,241
George Reith
Author by

George Reith

Updated on July 17, 2020

Comments

  • George Reith
    George Reith almost 4 years

    this question looks like it should have a simple answer but google and the php manual are being no help to me, maybe I just don't understand what they are telling me.

    I have an array example:

    $outcomes_array = array(1,4,2,3,5);
    

    It will always contain just numbers, how do I go about sorting this array so that it is always in descending order?

    so output I want:

    $outcomes_array[0] = 5
    $outcomes_array[1] = 4
    $outcomes_array[2] = 3
    

    and so on...

    Thanks :)

  • George Reith
    George Reith almost 13 years
    Thanks I knew it must of been simple :P