使用php在多维数组中按键排序 [英] Sorting by key in a multidimensional array with php

查看:394
本文介绍了使用php在多维数组中按键排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在PHP中排序多维数组

Possible Duplicate:
Sorting multidimensional array in PHP

如何按多维数组中的键排序?

How can I sort by key in a multidimensional array?

例如,下面是我从数据库中打印的数组,其中最新排在第一位- 12月,11月,10月等 2011、2010年,2009等

For instance, below is the array I print from my db, where the latest comes first - December, November, October, etc and 2011, 2010, 2009, etc

Array
(
    [0] => Array
        (
            [URL] => september 2011
            [Title] => September 2011
            [Date] => 8
            [Month] => 9
            [Year] => 2011
        )

    [1] => Array
        (
            [URL] => january 2011
            [Title] => January 2011
            [Date] => 1
            [Month] => 2
            [Year] => 2011
        )

    [2] => Array
        (
            [URL] => february 2011
            [Title] => February 2011
            [Date] => 4
            [Month] => 1
            [Year] => 2011
        )

    [3] => Array
        (
            [URL] => november 2011
            [Title] => November 2011
            [Date] => 23
            [Month] => 11
            [Year] => 2010
        )

    [4] => Array
        (
            [URL] => april 2011
            [Title] => April 2011
            [Date] => 23
            [Month] => 4
            [Year] => 2010
        )

)

但是我需要这样, 10月,11月,12月等 2011、2010、2009等-请注意,月份按最早的排在第一位,但年份仍按最新的排在第一位.

But I need it to be like this, October, November, December, etc and 2011, 2010, 2009, etc - note the months are sorted by the oldest comes first but the years are still sorted by the latest comes first.

所以数组应该像这样排序

So the array should be sorted like this,

Array
(

    [2] => Array
        (
            [URL] => february 2011
            [Title] => February 2011
            [Date] => 4
            [Month] => 1
            [Year] => 2011
        )

    [1] => Array
        (
            [URL] => january 2011
            [Title] => January 2011
            [Date] => 1
            [Month] => 2
            [Year] => 2011
        )

    [0] => Array
        (
            [URL] => september 2011
            [Title] => September 2011
            [Date] => 8
            [Month] => 9
            [Year] => 2011
        )

    [4] => Array
        (
            [URL] => april 2010
            [Title] => April 2010
            [Date] => 23
            [Month] => 4
            [Year] => 2010
        )

    [3] => Array
        (
            [URL] => november 2010
            [Title] => November 2010
            [Date] => 23
            [Month] => 11
            [Year] => 2010
        )
)

有可能吗?

推荐答案

用于对具有多个键的数组进行排序的通用解决方案

根据我对这个问题的回答,这是一个非常通用的解决方案,可以在很多情况下使用.

Generic solution to sort arrays of arrays with multiple keys

Based on my answer to this question, here is a very generic solution that you can use in lots of situations.

限制::由于存在匿名函数,需要PHP> = 5.3才能工作.

Limitation: Requires PHP >= 5.3 to work, due to the presence of anonymous functions.

function make_comparer() {
    $criteriaNames = func_get_args();
    $comparer = function($first, $second) use ($criteriaNames) {
        // Do we have anything to compare?
        while(!empty($criteriaNames)) {
            // What will we compare now?
            $criterion = array_shift($criteriaNames);

            // Used to reverse the sort order by multiplying
            // 1 = ascending, -1 = descending
            $sortOrder = 1; 
            if (is_array($criterion)) {
                $sortOrder = $criterion[1] == SORT_DESC ? -1 : 1;
                $criterion = $criterion[0];
            }

            // Do the actual comparison
            if ($first[$criterion] < $second[$criterion]) {
                return -1 * $sortOrder;
            }
            else if ($first[$criterion] > $second[$criterion]) {
                return 1 * $sortOrder;
            }

        }

        // Nothing more to compare with, so $first == $second
        return 0;
    };

    return $comparer;
}

使用方法

按升序排列:

How to use it

To sort by year ascending:

uasort($array, make_comparer('Year'));

要按年份升序排序,然后按月份升序排序:

To sort by year ascending, then by month ascending:

uasort($array, make_comparer('Year', 'Month'));

要按年份降序排序,然后按月升序排序:

To sort by year descending, then by month ascending:

uasort($array, make_comparer(array('Year', SORT_DESC), 'Month'));

这最后一个是你想要的.

This last one is what you 're after.

这篇关于使用php在多维数组中按键排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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