我已經在SOF上查到過,但無法找到它的解決方案。我有日期這是month
和year
像這樣來自mysql。我的SQL查詢使用日期格式這樣DATE_FORMAT(month, '%m %Y')
在PHP或MySQL中排序日期(月份年份)
[0] => Array
(
[month] => 12 2016
[bundle_Price] => 6000
)
[1] => Array
(
[month] => 07 2017
[bundle_Price] => 1200
)
這裏是我的排序功能..
$MyResult = Company::Sorting($Result,'month');
public static function vpbxSorting_2($a,$subkey) {
foreach($a as $k=>$v) {
$b[$k] = strtolower($v[$subkey]);
}
asort($b, 1);
foreach($b as $key=>$val) {
$c[] = $a[$key];
}
return $c;
}
它給我的排序是這樣這是不正確的..
print_r($MyResult);
如下排序:
Array
(
[0] => Array
(
[month] => 01 2017
[bundle_Price] => 1200
)
[1] => Array
(
[month] => 02 2017
[bundle_Price] => 7200
)
[2] => Array
(
[month] => 04 2017
[bundle_Price] => 1200
)
[3] => Array
(
[month] => 04 2016
[bundle_Price] => 7200
)
[4] => Array
(
[month] => 05 2017
[bundle_Price] => 1200
)
[5] => Array
(
[month] => 05 2016
[bundle_Price] => 13200
)
[6] => Array
(
[month] => 06 2017
[bundle_Price] => 9600
)
[7] => Array
(
[month] => 06 2016
[bundle_Price] => 6000
)
[8] => Array
(
[month] => 07 2017
[bundle_Price] => 14400
)
[9] => Array
(
[month] => 07 2016
[bundle_Price] => 6000
)
[10] => Array
(
[month] => 08 2017
[bundle_Price] => 1200
)
[11] => Array
(
[month] => 08 2016
[bundle_Price] => 6000
)
[12] => Array
(
[month] => 09 2016
[bundle_Price] => 17500
)
[13] => Array
(
[month] => 10 2016
[bundle_Price] => 1200
)
[14] => Array
(
[month] => 12 2016
[bundle_Price] => 6000
)
)
,但我希望它按照DESY順序MonthYear排序...排序後,上述陣列中的順序是不正確的。
下面的排序順序是月份和年份的順序。
.. so on....
[month] => 03 2017
[month] => 02 2017
[month] => 01 2017
[month] => 12 2016
[month] => 11 2016
[month] => 10 2016
[month] => 09 2016
... so on...
我想[month]
場陣列命令,而不是由[bundle_price
],但bundle_price應該陣列在那裏。
我不明白。輸出是預期的結果嗎?另外,您想按月逐月訂購,但bundle_price無關緊要? – samayo
我想按數組中的[[month]'字段排序,而不是'[bundle_price'],但bundle_price應該在數組中。 –
今年呢?它應該是月份,然後是年份,否則會有這樣的'03 2017,04 2016,05 2017 ...' – samayo