0

I have a PHP array which has the set of users. Each user belongs to a team. The array would be looking like below.

$Array = ARRAY('User1', 'User2', 'User3', 'User4', 'User5');

I need to arrive the below logic.

If 'User1' and 'User2' belong to same team named 'Team1' and the other Users are having separate unique teams then the array should be reordered as below.

$Array = ARRAY('User1', 'User3', 'User4', 'User5', 'User2');

If 'User1' and 'User2' belong to same team named 'Team1' and 'User3' and 'User4' belong to same team named 'Team2' then array should reordered as below.

$Array = ARRAY('User1', 'User3', 'User5', 'User4', 'User2');

Number is users is dynamic and any user can have any team. The aim is to maximum avoid the Users from same team in subsequent places. It is better to keep Users from same team as far as we can.

I have tried many ways, but nothing works out. Please help me to arrive this logic. Please let me know if anybody need more info.

Thanks Ram

1 Answer 1

1

It's not really clear what your goals are. What should be done when User1+2 and User3+4+5 are in the same group (I assume: 3,1,4,2,5)? Any routine will fail when User2+3+4+5 are in the same group.

One approach for the goal "avoid users from same team in subsequent places" could be:

Create arrays for the groups and collect their users. Fill a new array of users with the first user of the group with the most users, drop the user from the group array. Then add to the user array the first user of the group with the current most users but except the last used group. And so on.

Sign up to request clarification or add additional context in comments.

Comments

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.