使用 php 在多维数组中按键排序

发布于 2024-12-05 02:45:41 字数 2607 浏览 0 评论 0原文

可能的重复:
在 PHP 中对多维数组进行排序

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

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

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
        )

)

但我需要它像这样,十月、十一月、十二月等2011年、2010年、2009年等 - 请注意,月份按最旧在前排序,但年份仍按最新在前排序。

所以数组应该这样排序,

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

这可能吗?

Possible Duplicate:
Sorting multidimensional array in PHP

How can I sort by key in a multidimensional array?

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
        )

)

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

Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぶ宁プ宁ぶ 2024-12-12 02:45:41

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

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

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

新增和改进,现在支持降序排序

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;
}

如何使用它

按年份升序排序:

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

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

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

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

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

最后一个就是您想要的。

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.

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

New and improved, now with descending sort support

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.

静赏你的温柔 2024-12-12 02:45:41

假设您在问题中提供的数据实际上是正确的(以年、月、日为基础来创建排序),您可以首先按这些值索引数组并对主数组进行排序。我只是写它,否则你可能会误读 Demo 的输出,URL/标题不对应给定的数值,但它只是有效:

// create an index by date
foreach($data as $k=>$v)
{
    $index[$k] = sprintf('%04d-%02d-%02d', $v['Year'], $v['Month'], $v['Date']);
}

// sort data based on the index
array_multisort($index, SORT_DESC, $data);

Assuming that the data you would have provided in your question would have been actually correct (taking Year, Month, Date as base to create a sort on), you can first index the array by those values and the sort the main array. I'm just writing it, otherwise you might misread the output of the Demo, the URL/Titles do not correspond to the numerical values given, but it just works:

// create an index by date
foreach($data as $k=>$v)
{
    $index[$k] = sprintf('%04d-%02d-%02d', $v['Year'], $v['Month'], $v['Date']);
}

// sort data based on the index
array_multisort($index, SORT_DESC, $data);
溺ぐ爱和你が 2024-12-12 02:45:41

怎么样:

数据:

$arr = Array (
    Array (
        'URL' => 'september 2011',
        'Title' => 'September 2011',
        'Date' => 8,
        'Month' => 9,
        'Year' => 2011,
    ),
    Array (
        'URL' => 'january 2011',
        'Title' => 'January 2011',
        'Date' => 1,
        'Month' => 2,
        'Year' => 2011,
    ),
    Array (
        'URL' => 'february 2011',
        'Title' => 'February 2011',
        'Date' => 4,
        'Month' => 1,
        'Year' => 2011,
    ),
    Array (
        'URL' => 'november 2011',
        'Title' => 'November 2011',
        'Date' => 23,
        'Month' => 11,
        'Year' => 2010,
    ),
    Array (
        'URL' => 'april 2011',
        'Title' => 'April 2011',
        'Date' => 23,
        'Month' => 4,
        'Year' => 2010,
    ),
);

代码:

function compare($a, $b) {
    if ($a['Year'] == $b['Year']) {
      return ($a['Month'] - $b['Month']);
    } else {
        return ($b['Year'] - $a['Year']);
    }
}    
usort($arr, 'compare');
print_r($arr);    

输出:

Array
(
[0] => 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
    )

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

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

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

)

How about:

Data:

$arr = Array (
    Array (
        'URL' => 'september 2011',
        'Title' => 'September 2011',
        'Date' => 8,
        'Month' => 9,
        'Year' => 2011,
    ),
    Array (
        'URL' => 'january 2011',
        'Title' => 'January 2011',
        'Date' => 1,
        'Month' => 2,
        'Year' => 2011,
    ),
    Array (
        'URL' => 'february 2011',
        'Title' => 'February 2011',
        'Date' => 4,
        'Month' => 1,
        'Year' => 2011,
    ),
    Array (
        'URL' => 'november 2011',
        'Title' => 'November 2011',
        'Date' => 23,
        'Month' => 11,
        'Year' => 2010,
    ),
    Array (
        'URL' => 'april 2011',
        'Title' => 'April 2011',
        'Date' => 23,
        'Month' => 4,
        'Year' => 2010,
    ),
);

code:

function compare($a, $b) {
    if ($a['Year'] == $b['Year']) {
      return ($a['Month'] - $b['Month']);
    } else {
        return ($b['Year'] - $a['Year']);
    }
}    
usort($arr, 'compare');
print_r($arr);    

output:

Array
(
[0] => 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
    )

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

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

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

)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文