Question

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

Was it helpful?

Solution

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

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:

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top