简体   繁体   English

PHP排序多维数组

[英]PHP sort multidimensional array

I have an array that looks like this: 我有一个看起来像这样的数组:

Array
(
[13] => Array
        (
            [name] => Blah blah
            [description] => Blah blah blah
            [parent_group_id] => 8
            [display] => Blah : Blah
            [stamps] => Array
                (
                    [73] => Array
                        (
                            [stamp_id] => 73
                            [name] => Blah
                            [is_member] => 
                            [rank] => 2          <--
                            [description] => Blah blah blah
                            [rank_token] => easy
                        )
                    [80] => Array
                        (
                            [stamp_id] => 80
                            [name] => Blah
                            [is_member] => 
                            [rank] => 1          <--
                            [description] => Blah blah blah
                            [rank_token] => medium
                        )
                    [73] => Array
                        (
                            [stamp_id] => 73
                            [name] => Blah
                            [is_member] => 
                            [rank] => 4          <--
                            [description] => Blah blah blah
                            [rank_token] => easy
                        )

                    [80] => Array
                        (
                            [stamp_id] => 80
                            [name] => Blah
                            [is_member] => 
                            [rank] => 3          <--
                            [description] => Blah blah blah
                            [rank_token] => medium
                        )
                 )
          )
)

However, I want to sort the objects in this array by the numbers that ['rank'] holds, which are 1, 2, 3, and 4. (I have added arrows in the code examples) So after I use usort, I want everything to be sorted out in numerical order. 但是,我想按['rank']所持有的数字(分别为1、2、3和4)对这个数组中的对象进行排序。(我在代码示例中添加了箭头)因此在使用usort之后,我希望所有内容都按数字顺序进行整理。 So I would want it to look like this: 所以我希望它看起来像这样:

Array
(
[13] => Array
        (
            [name] => Blah blah
            [description] => Blah blah blah
            [parent_group_id] => 8
            [display] => Blah : Blah
            [stamps] => Array
                (
                    [80] => Array
                        (
                            [stamp_id] => 80
                            [name] => Blah
                            [is_member] => 
                            [rank] => 1          <--
                            [description] => Blah blah blah
                            [rank_token] => medium
                        )
                    [73] => Array
                        (
                            [stamp_id] => 73
                            [name] => Blah
                            [is_member] => 
                            [rank] => 2          <--
                            [description] => Blah blah blah
                            [rank_token] => easy
                        )
                    [80] => Array
                        (
                            [stamp_id] => 80
                            [name] => Blah
                            [is_member] => 
                            [rank] => 3          <--
                            [description] => Blah blah blah
                            [rank_token] => medium
                        )
                    [73] => Array
                        (
                            [stamp_id] => 73
                            [name] => Blah
                            [is_member] => 
                            [rank] => 4          <--
                            [description] => Blah blah blah
                            [rank_token] => easy
                        )
                 )
          )
)

My actual array is much bigger, however it still follows this pattern. 我的实际数组更大,但是它仍然遵循这种模式。

This should work, if I have understood your question correctly: 如果我正确理解了您的问题,这应该可以工作:

function cmp($a, $b)
{
    if ($a['rank'] == $b['rank']) {
        return 0;
    }
    return ($a['rank'] < $b['rank']) ? -1 : 1;
}
foreach($yourArray as &$entry) {
    uasort($entry['stamps'], "cmp");
}
unset($entry);

print_r($yourArray);

Note that the cmp function is almost the same as in the manual. 请注意,cmp功能与手册中的功能几乎相同。 The ampersand in the foreach means that the variable created is an alias of the array member instead of a copy (as PHP usually does). foreach中的&表示所创建的变量是数组成员的别名,而不是副本的别名(如PHP通常那样)。 The unset() is there because if you try to use a variable named $entry later, you will actually be manipulating the last entry in the array. 之所以存在unset()是因为,如果以后尝试使用名为$ entry的变量,则实际上是在操纵数组中的最后一个条目。

If you are not comfortable with this, there are other ways to skin it; 如果您对此感到不舒服,则可以通过其他方法对其进行皮肤处理。 for example, you could create a second function and array_map it to your original, thus: 例如,您可以创建第二个函数并将array_map映射到原始函数,从而:

function cmp($a, $b)
{
    if ($a['rank'] == $b['rank']) {
        return 0;
    }
    return ($a['rank'] < $b['rank']) ? -1 : 1;
}
function sort_entry($entry)
{
    uasort($entry['stamps'], "cmp");
    return $entry;
}
array_map('sort_entry', $yourArray);    
print_r($yourArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM