0

我有一个看起来像这样的数组:

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

但是,我想通过 ['rank'] 持有的数字对这个数组中的对象进行排序,分别是 1、2、3 和 4。(我在代码示例中添加了箭头)所以在使用 usort 之后,我希望一切都按数字顺序排序。所以我希望它看起来像这样:

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

我的实际数组要大得多,但它仍然遵循这种模式。

4

1 回答 1

1

如果我正确理解了您的问题,这应该可行:

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

请注意,cmp 功能与手册中的几乎相同。foreach 中的 & 表示创建的变量是数组成员的别名,而不是副本(就像 PHP 通常做的那样)。unset() 存在是因为如果您稍后尝试使用名为 $entry 的变量,您实际上将操作数组中的最后一个条目。

如果您对此不满意,还有其他方法可以剥皮;例如,您可以创建第二个函数并将其 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);
于 2013-03-12T22:12:29.873 回答