0

I am trying to sort an array that contains numbers that range in substantial values. The result I want to get is a descending order of those numbers from the array I am retrieving from a MySQL Server. So far I have created this to test out the "sort" function:

<?php

$numbers = array("100", "50", "70", "1000");
sort($numbers);
echo var_dump($numbers);

?>

And the result I get is this:

array(4) { [0]=>  string(2) "50" [1]=>  string(2) "70" [2]=>  string(3) "100" [3]=>  string(4) "1000" } 

I can see that the numbers are listing from smallest to largest, but I want it to list from the biggest integer to the smallest integer.

1
  • Because these values are all strings, and you want them sorted as numeric values, remember to use the SORT_NUMERIC flag with sort() or rsort() Commented Aug 25, 2010 at 14:46

3 Answers 3

2

You need rsort to sort in reverse order:

rsort($numbers);

More Info:

Sign up to request clarification or add additional context in comments.

Comments

1

rsort() reverse sorts the array :)

3 Comments

Alright, but how do I get rid of the other text, and just include the numbers?
@Kevin - What do you mean the "other text"? Do you mean the additional text generated by var_dump listing the data type, key values, etc?
its not "other text" try $array[0]... it should just return the number. Thats just the definition of the string type.
1

you can use rsort to sort it descending.

http://www.developertutorials.com/tutorials/php/sorting-array-php-051114-1019/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.