This is just an extension of the Truth answer based on comments:
The first thing I would do differently is basic player tracking.
$unassignedPlayers = $fighterList;
How the algorithm will work like this: prepare a list of commands (if you use a database, use SELECT DISTINCT or GROUP BY teams.id ):
$teams = array(); foreach( $fighterList as $fighter){ $teams[] = $figter->team; } $teams = array_unique( $teams);
Next, we need a method that will divide the array of fighters (let's say we have teams {A,A,B,B,C,C} that we want to split into {A,A} , {B,B,C,C} ):
Now that we have this, we can create a function that will sort the team members:
function assignFighters( array &$input, array $teams, array &$output){
Up to this point, you could use the Truth algorithm, but this should have a better weight match and represent what you intend to more clearly, but in any case $unassignedPlayers now comes to work:
$assignedPlayers = array(); $state = assignFighters( $unassignedPlayers, $teams, $assignedPlayers);
So what now ... If you have $state === false resp. count( $unassignedPlayers) > 0 something went wrong and we need to apply magic. How will this magic work:
I wrote all this from my head (it was a challenge), test it and leave notes in the comments, please :)