PHP - 多uasort功能突破整理 [英] PHP - Multiple uasort functions breaks sorting

查看:121
本文介绍了PHP - 多uasort功能突破整理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储人的多维数组。

I have a multidimensional array that stores people.

Array (
   id93294 => (array (
             Name => "Tom Anderson",
             Birthday => "03/17/1975"),
   id29349 => (array (
             Name => "Tom Anderson",
             Birthday => "03/17/1975")
)

有点像,除了为人民更多的信息,所以我想先排序生日再由另一个属性排序(如果他们的家乡符合其当前位置),但一旦我做了第二次排序阵列上它失去了第一类我没有使用生日...

Kind of like that except with more info for the people, so I want to first sort by birthdays THEN sort by another attribute (if their hometown matches their current location) but once I do the second sort on the array it loses the first sort I did using the birthdays...

我有点多次如何没有它搞乱了我的previous排序。

How do I sort multiple times without it messing up my previous sorts.

P.S。我使用uasort。

P.S. I am using uasort.

推荐答案

我最近<一个href=\"http://stackoverflow.com/questions/96759/how-do-i-sort-a-multidimensional-array-in-php/16788610#16788610\">answered这个问题在功能更加实用的方式,在权威主题的排序多维数组。你可以跳过阅读这个答案的其余部分,直接按照链接,一个更强大的多的解决方案。

Update

I recently answered this question in a much more capable manner in the "definitive" topic on sorting multidimensional arrays. You can safely skip reading the rest of this answer and directly follow the link for a much more capable solution.

功能 uasort ,您可以定义自己的比较函数。简单地说所有你想要的内部的标准。

The function uasort lets you define your own comparison function. Simply put all the criteria you want inside that.

例如,生日,然后按名称排序:

For example, to sort by birthday and then by name:

function comparer($first, $second) {
    // First see if birthdays differ
    if ($first['birthday'] < $second['birthday']) {
        return -1;
    }
    else if ($first['birthday'] > $second['birthday']) {
        return 1;
    }

    // OK, birthdays are equal. What else?
    if ($first['name'] < $second['name']) {
        return -1;
    }
    else if ($first['name'] > $second['name']) {
        return 1;
    }

    // No more sort criteria. The two elements are equal.
    return 0;
}

我忽略了一个事实,在你的榜样,生日不在,可以通过一个简单的对比使用操作&LT订购的格式; 。在实践中,你会将其转换为一个平凡的,比较的格式第一。

I am ignoring the fact that in your example, the birthdays are not in a format that can be ordered by a simple comparison using the operator <. In practice you would convert them to a trivially-comparable format first.

更新:如果您认为维护一堆这些多标准comparers能拿丑陋实快,你找我同意。但是,这个问题可以在计算机科学来解决任何其他:只需添加另一个抽象级别

Update: if you think that maintaining a bunch of these multiple-criteria comparers could get ugly real fast, you find me in agreement. But this problem can be solved as any other in computer science: just add another level of abstraction.

我就假设了PHP 5.3的下一个例子,为了使用方便的匿名函数的语法。但在原则上,你可以做同样的 create_function

I 'll be assuming PHP 5.3 for the next example, in order to use the convenient anon function syntax. But in principle, you could do the same with create_function.

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

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

        }

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

    return $comparer;
}

您可以然后执行:

uasort($myArray, make_comparer('birthday', 'name'));

这个例子可能会试图太聪明;一般我不喜欢使用不按名称接受其参数的函数。但是,在这种情况下,使用场景是太聪明了非常有力的论据。

This example possibly tries to be too clever; in general I don't like to use functions that do not accept their arguments by name. But in this case, the usage scenario is a very strong argument for being too clever.

这篇关于PHP - 多uasort功能突破整理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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