views:

43

answers:

4

Hello everyone,

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. Also I don't understand why it has text other than the integers. If anyone could help me out on this, I would greatly appreciate it.

Thanks,

Kevin

+1  A: 

You need rsort to sort in reverse order:

rsort($numbers);

More Info:

Sarfraz
A: 

you can use rsort to sort it descending.

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

tarrasch
+1  A: 

rsort() reverse sorts the array :)

Thomas Clayson
Alright, but how do I get rid of the other text, and just include the numbers?
Kevin
@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?
Mark Baker
its not "other text" try $array[0]... it should just return the number. Thats just the definition of the string type.
Thomas Clayson
A: 

Let me show you can find an answer yourself.

  1. navigate to the manual page for the function you are currently using: http://php.net/sort
    note especially easy address - just eight characters and a function name. Very handy.

  2. scroll down to the See also section.

  3. Pick appropriate function.

  4. Done!

See, it's not that hard. And no need to accept any answers, cause you answered question yourself.

As for the text, there isn't any. Just try to use this array for something useful and see

Col. Shrapnel