dongxin991209 2017-10-17 00:55
浏览 75
已采纳

根据值按月对数组进行排序

I have a php array as following -

$testar = array( 
 '3423sdfskjx' => 'January 2017',
 '1233sd3dkjx' => 'December 2017',
 '1534grfdbfd' => 'March 2017',  
 '5849dj4fodo' => 'April 2017',
 'ndj3058rjei' => 'February 2017',
 'lsdl39430xm' => 'September 2017',
 '059dmejri30' => 'July 2017',
 'mcjd923kd05' => 'November 2017',
 '3409sndfk3k' => 'May 2017',
 '094873uv3jj' => 'June 2017',
 '7859349cmei' => 'October 2017',
 '086u7n3if39' => 'August 2017');

Primarily key is a random id and value representing some month value. I need to be sort it in alphabetical order of values so that array gets reorganized where the first key value is 'January 2017', second key value is 'February 2017' and so forth. Standard use of sort/rsort/asort/ksort won't work. How do I get this array to be sorted by months based on the array element value?

  • 写回答

2条回答 默认 最新

  • doushang1964 2017-10-17 01:20
    关注

    You will need to define a custom function for sorting your array

    function sortByDate($a, $b) {
         return strtotime($a) - strtotime($b);
    }
    
    usort($testar, 'sortByDate');
    

    I have derived the answer from this one

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀