有序对象数组按照3个字母的月顺序在php [英] ordered object array as per 3 letter month order in php

查看:117
本文介绍了有序对象数组按照3个字母的月顺序在php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个对象数组的数据库数据的结果

i got a result of database's data of an object array

array(2) {
  [0]=>
   object(stdClass)#31 (1) {
           ["book_month"]=>
                     string(3) "Aug"
       }
    [1]=>
               object(stdClass)#32 (1) {
               ["book_month"]=>
                  string(3) "Jun"
    }
}

但我需要结果作为一个排序顺序像jan feb mar apr .....

but i need result as month as a sorting order like jan feb mar apr.....

我希望以下结果

 array(2) {
  [0]=>
   object(stdClass)#31 (1) {
           ["book_month"]=>
                     string(3) "Jun"
       }
    [1]=>
               object(stdClass)#32 (1) {
               ["book_month"]=>
                  string(3) "Aug"
    }
}


推荐答案

要扩展fvu的答案,下面是如何在php 5.3 +中实现解决方案

To expand on fvu's answer the following is how you would implement the solution in php 5.3+

$monthnames = array('Jan','Feb', 'Mar', 'Apr' 'May','Jun','Jul','Aug','Sep','Nov','Dec');
usort($array, function($a, $b) use ($monthnames) {
       return array_search($a->book_month, $monthnames) - array_search($b->book_month, $monthnames);
});

这篇关于有序对象数组按照3个字母的月顺序在php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆