AlecGuinn Posted December 26, 2021 Share Posted December 26, 2021 Hey, I am struggling to find a way to code an idea. I have a list of 10 peoples (possibly less) and i'd like to give them roles on a team. In this team, roles have differents importances. I have a roles array sort by importance. For each roles i have an array of people who can achieve this role base on a value (The lowest value having the highest priority) . Everyone CANT do every roles. I'd like to have the best team composition base on the role importance and the users roles values. Any help ? Ty Quote Link to comment https://forums.phpfreaks.com/topic/314357-role-distribution/ Share on other sites More sharing options...
gizmola Posted December 26, 2021 Share Posted December 26, 2021 What exactly are you having troubles with? Database design? Data structures? Coding? Some specific examples would help. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314357-role-distribution/#findComment-1593011 Share on other sites More sharing options...
AlecGuinn Posted December 27, 2021 Author Share Posted December 27, 2021 Its more about a coding problem on how i can put users on the good role. Im a php newby and i cant find a logic, a way to sort this the good way. $in_team = array(); foreach ($roles_array as $role_id => $role) { foreach ($users_roles[$role_id] as $user_id => $score) { if (!in_array($user_id, $in_team)) { $final_array[$role_id] = $user_id; $in_team[] = $user_id; break; } } } I end up with this: // Keys are role_id, values are user_id $final_array = Array ( [2] => 1 [10] => 613 [1] => 751 [6] => 709 [4] => 649 [3] => 797 [5] => 594 [8] => 445 ) But for exemple, i have lost role 7 which is a role with importance. The priority is to have users in roles with the highest importance and in this roles having the user with the lowest score (the ones on the top of the Users arrays), with 1 user by role. // Role list with keys as role_id, order by importance DESC $roles_array = Array ( [2] => Array ( [role_id] => 2 [importance] => 9 ) [10] => Array ( [role_id] => 10 [importance] => 9 ) [1] => Array ( [role_id] => 1 [importance] => 8 ) [6] => Array ( [role_id] => 6 [importance] => 8 ) [4] => Array ( [role_id] => 4 [importance] => 7 ) [7] => Array ( [role_id] => 7 [importance] => 7 ) [3] => Array ( [role_id] => 3 [importance] => 0 ) [5] => Array ( [role_id] => 5 [importance] => 0 ) [8] => Array ( [role_id] => 8 [importance] => 0 ) [9] => Array ( [role_id] => 9 [importance] => 0 ) ) // Users array by roles with role_id as key and a list of user_id (user_id as key) sort by user "score" the first one having the priority on the role. $users_roles = Array ( [2] => Array ( [1] => -10403083 [649] => -10103685 [445] => -10103190 [613] => -10103162 [594] => -10003838 ) [10] => Array ( [1] => -10101899 [613] => -10101799 [649] => -10101599 [445] => -10101299 ) [1] => Array ( [751] => -9106772 [613] => -9106679 [709] => -9106281 [1] => -9106179 [445] => -9106175 [594] => -9106087 [797] => -9006662 ) [6] => Array ( [751] => -9106772 [613] => -9106679 [709] => -9106281 [1] => -9106179 [445] => -9106175 [594] => -9106087 [797] => -9006662 ) [4] => Array ( [613] => -8205090 [649] => -8105661 [709] => -8105270 [1] => -8105188 [751] => -8104676 ) [7] => Array ( [613] => -8205090 [649] => -8105661 [709] => -8105270 [1] => -8105188 [751] => -8104676 ) [3] => Array ( [797] => -1506653 [751] => -1106899 [1] => -1106894 [649] => -1106793 [613] => -1106299 [709] => -1105790 [594] => -1006679 ) [5] => Array ( [594] => -1510886 [797] => -1210750 [613] => -1111984 [751] => -1111673 [1] => -1111193 [709] => -1110979 [445] => -1108879 [649] => -1108683 ) [8] => Array ( [594] => -1510886 [797] => -1210750 [613] => -1111984 [751] => -1111673 [1] => -1111193 [709] => -1110979 [445] => -1108879 [649] => -1108683 ) [9] => Array ( [594] => -1510886 [797] => -1210750 [613] => -1111984 [751] => -1111673 [1] => -1111193 [709] => -1110979 [445] => -1108879 [649] => -1108683 ) ) Thanks you Quote Link to comment https://forums.phpfreaks.com/topic/314357-role-distribution/#findComment-1593023 Share on other sites More sharing options...
mac_gyver Posted December 27, 2021 Share Posted December 27, 2021 of topic, but please use var_export() when posting sample data. Quote Link to comment https://forums.phpfreaks.com/topic/314357-role-distribution/#findComment-1593029 Share on other sites More sharing options...
mac_gyver Posted December 28, 2021 Share Posted December 28, 2021 the sample data, which was probably defined to demonstrate this issue, ran out of users that could perform role id 7 before reaching the role id 7 evaluation. when this occurs, you need to be willing to accept a less than ideal solution, by introducing a random element into the process. a general method would be to take the top n (n = 1, 2, 3, ...) initial entries in the $roles_array, shuffle it/them, and produce a 'current' array consisting with those shuffled entries and the remaining unshuffled entries and try to produce a solution using that data that satisfies all the roles that have required importance. repeat the shuffling for each n value at least n squared times to give the shuffling a chance to produce unique results (computers are not very good at doing things randomly.) after each shuffle, check if the key order has already been tried, by remembering the composite key sequences, that have been evaluated, in an array, by imploding the keys with some unique character, e.g. a | or similar. if the key order has already been tried, skip the current evaluation loop. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314357-role-distribution/#findComment-1593032 Share on other sites More sharing options...
gizmola Posted December 28, 2021 Share Posted December 28, 2021 It seems to me that mac_gyver has identified the key problem you have, which is that it appears you exhaust all the available players qualified for role 7 before arriving at role 7. This is equivalent to saying you have an open position for "chief surgeon" and only 4 candidates available, and you find that all 4 candidates already took other positions. What can you do if you have no qualified applicants? There is nothing to do -- the job sits open until you have more qualified applicants. If you want to insure that you place someone, you could, if a group has no person in the role, continue on to check the next group, and to keep doing that until either you filled the role Or you again have gotten to the end of the users_roles array. This will fill the role with the top available applicant in the following group(s) if possible. Quote Link to comment https://forums.phpfreaks.com/topic/314357-role-distribution/#findComment-1593047 Share on other sites More sharing options...
Zane Posted December 30, 2021 Share Posted December 30, 2021 What exactly is your desired output? You provided what $final_array looks like, but you didn't clarify if that's what you're expecting. Are you simply asking for a better strategy? Quote Link to comment https://forums.phpfreaks.com/topic/314357-role-distribution/#findComment-1593066 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.