排序数字字符串数组在PHP [英] sort numeric string array in php
本文介绍了排序数字字符串数组在PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个PHP数组,如:
I have a php array like :
myarr[1] = "1",
myarr[2] = "1.233",
myarr[3] = "0",
myarr[4] = "2.5"
值实际上是字符串,但是我想这个数组进行数字排序,还要考虑浮点值和维护索引关系。
the values are actually strings but i want this array to be sorted numerically, also considering float values and maintaining index association.
请帮助我。
谢谢
Please help me out. Thanks
推荐答案
您可以使用正常的 排序
功能。它需要第二个参数来告诉你如何想排序。选择 SORT_NUMERIC
。
You can use the normal sort
function. It takes a second parameter to tell how you want to sort it. Choose SORT_NUMERIC
.
例如:
sort($myarr, SORT_NUMERIC);
print_r($myarr);
打印
Array
(
[0] => 0
[1] => 1
[2] => 1.233
[3] => 2.5
)
更新:为了维持键值对,使用 ASORT
(采用相同的参数),例如输出:
Update: For maintaining key-value pairs, use asort
(takes the same arguments), example output:
Array
(
[3] => 0
[1] => 1
[2] => 1.233
[4] => 2.5
)
这篇关于排序数字字符串数组在PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文