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条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制