Tag:php task assignment system
<?php/** * Created by PhpStorm. * User: ziniu * Date: 2016/9/21 * time: 10:58 *////** * Task Description: * The small white family has five people, respectively is the father, Mother, brother, sister, * every day to carry out the family tasks are mopping, washing dishes, buy vegetables, which, washing dishes to do two times a day to do, * and buy food and must be a father or mother to buy! * Small white learning programming, small white father asked small white with the program to schedule a work schedule, try to do * everyone's task relative balance, in weeks can be! * * Task Analysis: * Issue 1: For tasks that can only be performed by parents, such as "buy vegetables", are similar to level issues * need to give the corresponding level of promotion to the appropriate person! * 2: For "dishwashing" This requires two missions per day, we can copy them directly, a * is similar to two "dishwashing" tasks. , each task executes only once a day, and it is done. * 3: For high-level tasks like "buy vegetables", you need to add the level of execution to your tasks. * * Special note: * This case in order to maximize the display of the basic stage of knowledge points, temporarily did not do code and structure optimization! */// first step:1.1 family member initialize separate definition $father = "Papa"; $mother = "Mom";$brother = "elder brother", $sister = "elder sister"; $myself = "Little White";// the first step:1.2 family members into an array, to become a whole, convenient programming operation $family = array ( $father, $mother, $brother, $sister, $myself);//second Step:2.1 task Initialize separate task $mission1 = "drag ground";// Once a day. $mission 2 = "Wash the dishes";// two times a day. $mission 3 = "Buy vegetables";// only parents to buy. The second step:2.2 combination becomes a task combination becomes a whole, convenient task operation $missions = array ( $mission 1, $mission 2, $mission 3);/** * Description: Because each task executes a different number of times per day, depending on the number of executions of the task * re-set the task array to a two-dimensional array with the number of executions. * @param $missions * @return array */function settimes ($missions) { $missionsTmp = []; foreach ($missions as $v) { switch ($v) { case "wash the dishes"; $missionsTmp [] = [' time ' =>2, ' name ' = > $v]; break; default: $MISSIONSTMP [] = [' time ' =>1, ' name ' = = $v]; break; } } return $MISSIONSTMP;} $missions = settimes ($missions);// execute function get new task array/** * Description: Depending on the number of tasks in each of the two-dimensional arrays set, Convert to a different * one-dimensional array * @param $missions * @return array */function getnewmissions ($missions) { $Temp = []; &Nbsp;foreach ($missions as $k => $v) { $Temp = array_merge ($Temp, Array_pad ([$v [' name '], $v [' time '], $v [' name ']); } return $Temp;} $missions = getnewmissions ($missions);/** * Description: According to the task requirements set the corresponding task and people's execution rights. * @param $family * @param $missions * @return array */ Function setlevel ($family, $missions) { // set member level $levelParent = 10; $levelOther = 1; // Set task requires level $ levelmissioneasy = 1; $levelMissionHard = 8; // Traverse family members foreach ($family as $k _f => $v _f) { $defaultLevel = $levelOther; switch ($v _f) {// switch Method Get Set the rating value case "Papa": $defaultLevel = $levelParent; break; case "Mom" : $defaultLevel = $ levelparent; break; } $family [$k _f] = [' personname ' + $v _f, ' Personlevel ' = $defaultLevel];// set new data } // traverse tasks foreach ($missions as $k _m => $v _m) { $ defaultmissionlevel = $levelMissionEasy;// Set the default level // if else Method if ($v _m == "buy food") { $defaultMissionLevel = $levelMissionHard; } $ missions[$k _m] = [' missionname ' = $v _m, ' missionlevel ' + $defaultMissionLevel];// set new data } return [' family ' and $family, ' mission ' + $missions];// return Data}$ Data = setlevel ($family, $missions);// as of the current position, all the "people" and "tasks" are completed// the next thing to do is, according to 7 days a week, The task is assigned. /** * Description: Task assignment Main program, incoming "person" and "task", return the corresponding sort result. * @param array $data * @return array */function makemission ( array $data) { // Initialize data $missionTable = [];// task Schedule $family = $data [' Family '];// family members $mission = $data [' Mission '];// task List // because of the number of times each person executes the task per week, the minimum number is chosen, so it is necessary toThe number of times the member performs a task to initialize. foreach ($family as $k => $v) { $v [' Persontimes '] = 0; $family [$k] = $v; } // If you plan to schedule 7 days of work tasks perform 7 day loops for ($i = 1; $i <= 7; $i + +) { // print hint string echo "********************************************************<br />"; echo "Choose to assign {$i} days task:<br />"; echo "********************************************************<br />"; $temp = [];// Initialize return results Array foreach ($ mission as $k => $v) {// cycle ensures that each task can be assigned to people // Print hint string echo "################### #任务分配开始 ######################<br /> "; echo "-- The "{$i}" day of the first. ($k + 1). " Task "{$v [' Missionname ']}" task assignment:<br /> "; // Get the best fit, by taking steps to get the right candidate, and the minimum task for the person to achieve. $tmp = array ( ' missionname ' => $v [' Missionname '], ' PersonName ' => getfamilyname ($family , $v) ); // for the selected person, perform the operation task add Operation foreach ($family as $k _f => $v _f ) { if ($v _f[' personname ']== $tmp [' personname ']) { $family [$k _f][' Persontimes ']++; } } $temp [] = $tmp;// task assignments are completed and deposited into the temporary data Warehouse, Represents a task assignment complete. // Print hint string echo "task assigned to {$tmp [' personname ']}<br />"; echo "################### #任务分配结束 ######################<br />"; echo "<br /><br />"; } $missionTable [] = $temp;// represents a day of task assignments complete. }// represents a week of assignment completion. return $missionTable;// Return Data}/** * Description: Depending on the level of the task, select the candidate that can perform the task. * @param $family * @param $mission * @return mixed */ Function getfamilyname ($family, $mission) { $tmp = [];// Storage warehouse for people who meet mission standards foreach ($family as $k => $v) { if ($ v[' Personlevel '] >= $mission [' Missionlevel ']) { $tmp [] = $v; } } // Sort the right people by the number of times they are performing tasks from small to large $person = null;//initialization Sort list $person = oderby ($tmp); return $person [0][' PersonName '];// the name of the person who returned the minimum number of task executions}/** * Description: According to the personnel list, find the person who has the fewest tasks at present. is the multidimensional array sorting problem * @param $family * @return mixed */function odErby ($family) { $sort = array ( ' direction ' = > ' Sort_asc ', //sort order flag SORT_DESC descending;sort_asc Ascending ' field ' => ' persontimes ',//Sort field );// storage Options $arrSort = array (); foreach ($family AS $uniqid = > $row) {// adjustment Sort string foreach ($row AS $key =>$ Value) { $arrSort [$key] [$uniqid] = $value; } } if ($sort [' direction ']) {// If you have a collation array_multisort ($arrSort [$sort [' Field ']], constant ($sort [' direction '] ), $family); }// perform sort processing return $family;} Formatting Displays the final result echo "<pre /> "; Var_dump (Makemission ($data));
This article is from the "Focus on PHP Group number: 414194301" blog, please be sure to keep this source http://jingshanls.blog.51cto.com/3357095/1856896
[Li Jingshan php]php implements a simple task assignment system