0

I have an array of players, which are arrays themselves and I want to sort by score.

[player_info] => Array
(
    [0] => Array
        (
            [player_] => âlêj!!tâ~ôk
            [score_] => 66
            [ping_] => 
            [team_] => Blue
        )

    [1] => Array
        (
            [player_] => alejitbella
            [score_] => 3
            [ping_] => 
            [team_] => Blue
        )

    [2] => Array
        (
            [player_] => §Uph§£lMejo
            [score_] => 34
            [ping_] => 
            [team_] => Red
        )

    [3] => Array
        (
            [player_] => timoleon
            [score_] => 10
            [ping_] => 
            [team_] => Red
        )

)

I've already managed to get usort working properly (not shown in the example above) however I need to preserve placement of teams. Let's say Simon, Becky, Alek, and George are here.

Simon 25 Red
Becky 13 Red
Alek 3 Blue
George 5 Blue

I want to be able to arrange them by score high to low, while preserving their location in the array based on team.

How might I accomplish this?

EDIT: Due to people thinking this is a duplicate I need to clarify.

I want to order these by score while preserving the order of the team "Red" and "Blue"

Basically I want to turn the above example into Simon 25 Red Becky 13 Red George 5 Blue Alek 3 Blue

but2002
  • 3
  • 2
  • Any chance you could make your expected output match the array in your question? Also, can't you just modify the usort function to include the team they are on first, then the score? (I am guessing that's what you want). – Fluffeh Nov 07 '13 at 02:15
  • Where does the data come from? It would be easier to do that in the database if it comes from a database... – jeroen Nov 07 '13 at 02:15
  • The data comes from a live game server. No database so I can't modify the order there. How would I go about that Fluffeh? – but2002 Nov 07 '13 at 02:25
  • You need to keep they array keys, right? Honestly... to keep it simple and use answers already out there that are easy to find... I would save the key in the array with the player info, then sort it using whatever code you want, and then put the key back... Does that makes sense? I tried to search for you but sorting a multiple dimensional array by more than one value while maintaining the keys is a hard find. – gloomy.penguin Nov 07 '13 at 04:06
  • Or - try your `usort()` callable function with [`uasort()`](http://us3.php.net/uasort) and see if you can make that work..? – gloomy.penguin Nov 07 '13 at 04:10

1 Answers1

0

Hope I understood correctly. This is my canned function for this. Since you didn't show the usort() I don't know if it is still needed or how it will work with this:

function array_column_sort(&$array, $column, $sort=SORT_ASC) {
    foreach($array as $key => $val) {
        $sort_array[$key] = $val[$column];
    }
    array_multisort($sort_array, $sort, $array);
}

// run yours like this
array_column_sort($array['player_info'], 'score_', SORT_DESC);

$sort flags are the same as defined for array_multisort()

To keep the team (no time to put in a function, your exercise) try this (not tested):

foreach($array['player_info'] as $key => $val) {
    $score[$key] = $val['score_'];
    $team[$key]  = $val['team_'];
}
array_multisort($team, SORT_ASC, $score, SORT_DESC, $array['player_info']);
AbraCadaver
  • 73,820
  • 7
  • 55
  • 81
  • This works to sort by score, however it does not preserve the seperation of "Red" and "Blue" I need to to preserve these while sorting the scores within these groups. – but2002 Nov 08 '13 at 02:31
  • Edited. Didn't see that from your post. Not tested, but you seem to get it so you can adjust if there is an issue. – AbraCadaver Nov 08 '13 at 05:50