开发者

PHP - Sorting an Array

开发者 https://www.devze.com 2023-01-12 20:38 出处:网络
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.

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.

  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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号