0

可能重复:
PHP中的数组排序函数

我有一个这样的数组

Array
(
[May] => 10904
[Jan] => 9269
[Jun] => 3743
[Feb] => 3507
[Mar] => 6017
[Apr] => 4966
)

我怎样才能按月对它们进行排序。一月、二月、三月等……

我试过这个没有运气。

$ytdCommissions = array_flip($ytdCommissions);

usort($ytdCommissions,'monthCompare');
function monthCompare($a, $b)
{
    $months = array('JAN' => 1, 'FEB' =>2,'MAR' => 3,'APR' => 4,'MAY' => 5,'JUN' => 6,'JULY' => 7,'AUG' => 8, 'SEP' => 9, 'OCT' => 10, 'NOV' => 11, 'DEC' => 12);
    if($a == $b)
    {
        return 0;
    }
    return ($months[$a] > $months[$b]) ? 1 : -1;
}

谢谢!

4

6 回答 6

2

1.)您需要uksort在排序键时使用

2.)您应该比较大写键以匹配您的月份数组:

$a = strtoupper($a);
$b = strtoupper($b);
于 2012-07-12T21:05:29.920 回答
0
function monthCompare($a, $b)
{
    $months = array('JAN' => 1, 'FEB' =>2,'MAR' => 3,'APR' => 4,'MAY' => 5,'JUN' => 6,'JULY' => 7,'AUG' => 8, 'SEP' => 9, 'OCT' => 10, 'NOV' => 11, 'DEC' => 12);
    if($a[0] == $b[0])
    {
        return 0;
    }
    return ($months[$a[0]] > $months[$b[0]]) ? 1 : -1;

}


$ytdCommissions = array(
 array('MAY' , 10904),
 array('JAN' , 9269),
 array('JUN' , 3743),
 array('FEB' , 3507),
 array('MAR' , 6017),
 array('APR' , 4966)
);

usort($ytdCommissions,'monthCompare');
print_r($ytdCommissions);
  • $months您在:JANJanon中使用大写字母$ytdCommissions
  • 使用 usort 未正确构建数组$ytdCommissions- 查看我上面的示例
于 2012-07-12T21:13:36.530 回答
0

尝试像这样制作一系列月份:

$months = array("Jan", "Feb", "March", "etc...");

然后每个月给他们分配你喜欢的任何值:

$months[0] = 9269; // Jan
$months[2] = 6017; // March

然后月份只是$months数组的键,分别$months[0]JanuaryDecember$months[11]

于 2012-07-12T21:04:22.687 回答
0

您正在使用用于值的 sort 进行排序。应该使用uksort。还删除了您构建的数组,因为我认为它是多余的:

$arr = array(
"May" => 10904,
"Jan" => 9269,
"Jun" => 3743,
"Feb" => 3507,
"Mar" => 6017,
"Apr" => 4966,
);

uksort($arr,'monthCompare');

var_dump($arr);

function monthCompare($a, $b)
{
    //$months = array('JAN' => 1, 'FEB' =>2,'MAR' => 3,'APR' => 4,'MAY' => 5,'JUN' => 6,'JULY' => 7,'AUG' => 8, 'SEP' => 9, 'OCT' => 10, 'NOV' => 11, 'DEC' => 12);
    $aMonth = date("m", strtotime($a));
    $bMonth = date("m", strtotime($b));

    if($a == $b)
    {
        return 0;
    }

    return ($aMonth > $bMonth) ? 1 : -1;
}
于 2012-07-12T21:11:13.060 回答
0
$data = array(
    'May' => 10904,
    'Jan' => 9269,
    'Jun' => 3743,
    'Feb' => 3507,
    'Mar' => 6017,
    'Apr' => 4966,
);

$sort = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

uksort($data,
       function($value1, $value2) use ($sort) {
           return array_search($value1,$sort) > array_search($value2,$sort);
       }
);

var_dump($data);
于 2012-07-12T21:16:55.403 回答
0

这很烦人,但似乎没有其他办法。

这是我能算出的最短代码:

<?
$months[May] = 10904;
$months[Jan] = 9269;
$months[Jun] = 3743;
$months[Feb] = 3507;
$months[Mar] = 6017;
$months[Apr] = 4966;

for($m=1;$m<=12;$m++)
{
    $m_index[date("M",mktime(0,0,0,$m,1,2000))] = $m;
}

foreach($months as $i => $v)
{
    $months_out[$m_index[$i]] = $v;
}
ksort($months_out);
print_r($months_out);
?>

退货

Array
(
    [1] => 9269
    [2] => 3507
    [3] => 6017
    [4] => 4966
    [5] => 10904
    [6] => 3743
)
于 2012-07-12T21:18:08.027 回答