如何从 php 中的值数组中对特定字符串进行排序? [英] how to sort particular strings from an array of values in php?
本文介绍了如何从 php 中的值数组中对特定字符串进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$array = array("2011-September_38","2011-June_4","2010-November_9","2011-November_29","2010-December_19");
我想对这个数组字符串进行如下排序,它应该先对年份进行排序,然后再对月份进行排序,
i want to sort this array strings as following, it should sort the year first, then it should sort the month,
期望的输出
Array ( [0] => 2010-Marh_19
[1] => 2010-November_9
[2] => 2011-June_4
[3] => 2011-September_38
[4] => 2011-November_29 )
我尝试过一些东西,有人可以将我的函数绑定到排序年然后月 http://codepad.org/skEiUkTC
I've tried something, could anyone bind my function to sort year then month http://codepad.org/skEiUkTC
推荐答案
试试这个代码:
<?php
$array = array("2011-September_38","2011-June_4","2010-November_9","2011-November_29","2010-December_19");
function monthCompare($a, $b) {
$da = strtotime(strstr($a, '_', true));
$db = strtotime(strstr($b, '_', true));
return $da > $db;
}
usort($array, "monthCompare");
print_r($array);
?>
输出
Array
(
[0] => 2010-November_9
[1] => 2010-December_19
[2] => 2011-June_4
[3] => 2011-September_38
[4] => 2011-November_29
)
这篇关于如何从 php 中的值数组中对特定字符串进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文