0

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.

2
  • I think this will help: <stackoverflow.com/questions/6570146/…> Commented Mar 12, 2013 at 21:59
  • 1
    You mentioned usort already, what have you tried? foreach($array as $key => $value) usort($array[$key]['stamps'],'yourusortfunction'); Commented Mar 12, 2013 at 22:00

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

No prob, have a nice evening

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.