PHP - 对数组进行排序
我正在尝试对包含大量值的数字的数组进行排序。我想要得到的结果是我从 MySQL 服务器检索的数组中的这些数字的降序排列。到目前为止,我已经创建了这个来测试“排序”功能:
<?php
$numbers = array("100", "50", "70", "1000");
sort($numbers);
echo var_dump($numbers);
?>
我得到的结果是这样的:
array(4) { [0]=> string(2) "50" [1]=> string(2) "70" [2]=> string(3) "100" [3]=> string(4) "1000" }
我可以看到数字是从最小到最大列出的,但我希望它从最大整数到最小整数列出。我也不明白为什么它有整数以外的文本。如果有人能帮助我解决这个问题,我将不胜感激。
谢谢,
凯文
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
让我告诉你你可以自己找到答案。
导航到您当前正在使用的函数的手册页:http://php.net/sort
注意特别简单的地址 - 只有八个字符和一个函数名称。非常方便。
向下滚动到
另请参阅
部分。选择合适的函数。
完成!
看吧,这并不难。并且无需接受任何答案,因为您自己回答了问题。
至于文字,则没有。只是尝试使用这个数组来做一些有用的事情,看看
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
您可以使用 rsort 对其进行降序排序。
http://www.developertutorials.com/tutorials/php /sorting-array-php-051114-1019/
you can use rsort to sort it descending.
http://www.developertutorials.com/tutorials/php/sorting-array-php-051114-1019/
rsort()
对数组进行反向排序:)rsort()
reverse sorts the array :)您需要
rsort
以相反顺序排序:更多信息:
You need
rsort
to sort in reverse order:More Info: