ds11111111111111111 2011-12-02 02:40
浏览 57
已采纳

如何从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,

DESIRED OUTPUT

Array ( [0] => 2010-Marh_19 
        [1] => 2010-November_9 
        [2] => 2011-June_4 
        [3] => 2011-September_38
        [4] => 2011-November_29 )

I've tried something, could anyone bind my function to sort year then month http://codepad.org/skEiUkTC

  • 写回答

2条回答 默认 最新

  • douci7521 2011-12-02 02:51
    关注

    Try this code:

    <?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);
    ?>
    

    OUTPUT

    Array
    (
        [0] => 2010-November_9
        [1] => 2010-December_19
        [2] => 2011-June_4
        [3] => 2011-September_38
        [4] => 2011-November_29
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部