PHP - Sorting an Array
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 smalles开发者_C百科t 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
You need rsort
to sort in reverse order:
rsort($numbers);
More Info:
- http://php.net/manual/en/function.rsort.php
rsort()
reverse sorts the array :)
you can use rsort to sort it descending.
http://www.developertutorials.com/tutorials/php/sorting-array-php-051114-1019/
Let me show you can find an answer yourself.
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.scroll down to the
See also
section.Pick appropriate function.
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
精彩评论