-5

我正在尝试在 PHP 中对数组进行排序。请看我的数组:

Array
(
    [0] => Array
        (
            [id] => 8
            [date_start_month_name] => January
            [approved] => Accepted: yes
        )

    [1] => Array
        (
            [id] => 3
            [date_start_month_name] => January
            [approved] => Accepted: yes
        )

    [2] => Array
        (
            [id] => 2
            [date_start_month_name] => March
            [approved] => Accepted: yes
        )

    [3] => Array
        (
            [id] => 5
            [date_start_month_name] => April
            [approved] => Accepted: yes
        )

)

我想使用月份名称对数组进行排序,例如:一月并得到如下内容:

Array
(
    [0] => Array
        (
            [id] => 8
            [date_start_month_name] => January
            [approved] => Accepted: yes
        )

    [1] => Array
        (
            [id] => 3
            [date_start_month_name] => January
            [approved] => Accepted: yes
        )
)

我怎样才能做到这一点?提前感谢您的帮助。最好的!

4

2 回答 2

0

您可以使用usort函数来实现这一点,如下所示:

<?php
     // Sort the multidimensional array
     usort($results, "custom_sort");
     // Define the custom sort function
     function custom_sort($a,$b) {
          return $a['some_sub_var']>$b['some_sub_var'];
     }
?>
于 2013-02-06T09:59:48.520 回答
0

利用usort

usort($array, function($a, $b) {
    return $a['date_start_month_name'] > $b['date_start_month_name']
});
于 2013-02-06T09:59:08.243 回答