下面是我的数组,我想对它进行排序:
$arr = array('0-3 months', '9-12 months', '3-6 months', '6-9 months', '12-18 months', '18-24 months');
它以sort
函数响应:
Array
(
[0] => 0-3 months
[1] => 12-18 months
[2] => 18-24 months
[3] => 3-6 months
[4] => 6-9 months
[5] => 9-12 months
)
我想以下面的方式:
Array
(
[0] => 0-3 months
[1] => 3-6 months
[2] => 6-9 months
[3] => 9-12 months
[4] => 12-18 months
[5] => 18-24 months
)
任何解决方案?
由于
答案 0 :(得分:4)
您可以使用sort($arr, SORT_NUMERIC);
。 SORT_NUMERIC
将元素视为数字。
答案 1 :(得分:3)
你可以使用natsort($ arr);还
答案 2 :(得分:2)
答案 3 :(得分:1)
尝试
$arr = array('0-3 months', '9-12 months', '3-6 months', '6-9 months', '12-18 months', '18-24 months');
foreach($arr as $v) {
$e = explode('-', $v);
$n1[] = $e[0];
$n = explode(' ', $e[1]);
$n2[] = $n[0];
}
sort($n1);
sort($n2);
for($i=0; $i<count($n1); $i++) {
$newarr[] = $n1[$i].'-'.$n2[$i].' '.'months';
}
print_r($newarr);
输出: -
Array
(
[0] => 0-3 months
[1] => 3-6 months
[2] => 6-9 months
[3] => 9-12 months
[4] => 12-18 months
[5] => 18-24 months
)