Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php按ASC顺序排序数组值_Php_Arrays_Sorting - Fatal编程技术网

php按ASC顺序排序数组值

php按ASC顺序排序数组值,php,arrays,sorting,Php,Arrays,Sorting,下面是我的数组,我想对它进行排序: $arr = array('0-3 months', '9-12 months', '3-6 months', '6-9 months', '12-18 months', '18-24 months'); 它响应排序函数,如: Array ( [0] => 0-3 months [1] => 12-18 months [2] => 18-24 months [3] => 3-6 months [

下面是我的数组,我想对它进行排序:

$arr = array('0-3 months', '9-12 months', '3-6 months', '6-9 months', '12-18 months', '18-24 months');
它响应
排序
函数,如:

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
)
有什么解决办法吗


谢谢

你试过自然分类吗

sort($arr, SORT_NATURAL | SORT_FLAG_CASE);

来源:

你试过自然排序吗

sort($arr, SORT_NATURAL | SORT_FLAG_CASE);
来源:

您可以使用
排序($arr,sort\u NUMERIC)
SORT\u NUMERIC
将元素视为数字。

您可以使用
SORT($arr,SORT\u NUMERIC)<代码>排序\u数值
将元素视为数字。

尝试

$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);
试一试

您可以使用natsort($arr);另外

您可以使用natsort($arr);另外

它只能是
“x-y月”
还是有其他值?重叠呢?等等。它只能是
“x-y月”
,还是有其他值?重叠呢?等