<?php
$father = "爸爸";
$mother = "妈妈";
$brother = "哥哥";
$sister = "姐姐";
$myself = "小白";
$family = array(
$father,
$mother,
$brother,
$sister,
$myself
);
$mission1 = "拖地";
$mission2 = "洗碗";
$mission3 = "买菜";
$missions = array(
$mission1,
$mission2,
$mission3
);
function setTimes($missions){
$missionsTmp = [];
foreach($missions as $v){
switch($v){
case "洗碗";
$missionsTmp[] = ['time'=>2,'name'=>$v];
break;
default:
$missionsTmp[] = ['time'=>1,'name'=>$v];
break;
}
}
return $missionsTmp;
}
$missions = setTimes($missions);
function getNewMissions($missions){
$Temp = [];
foreach($missions as $k => $v){
$Temp = array_merge($Temp,array_pad([$v['name']],$v['time'],$v['name']));
}
return $Temp;
}
$missions = getNewMissions($missions);
function setLevel($family,$missions){
$levelParent = 10;
$levelOther = 1;
$levelMissionEasy = 1;
$levelMissionHard = 8;
foreach($family as $k_f => $v_f){
$defaultLevel = $levelOther;
switch($v_f){
case "爸爸":
$defaultLevel = $levelParent;
break;
case "妈妈":
$defaultLevel = $levelParent;
break;
}
$family[$k_f] = ['personName'=>$v_f,'personLevel'=>$defaultLevel];
}
foreach($missions as $k_m => $v_m){
$defaultMissionLevel = $levelMissionEasy;
if($v_m == "买菜"){
$defaultMissionLevel = $levelMissionHard;
}
$missions[$k_m] = ['missionName'=>$v_m,'missionLevel'=>$defaultMissionLevel];
}
return ['family'=>$family,'mission'=>$missions];
}
$data = setLevel($family,$missions);
function makeMission(array $data){
$missionTable = [];
$family = $data['family'];
$mission = $data['mission'];
foreach($family as $k => $v){
$v['personTimes'] = 0;
$family[$k] = $v;
}
for($i = 1; $i <= 7; $i++){
echo "********************************************************<br />";
echo "选择进行分配第{$i}天任务:<br />";
echo "********************************************************<br />";
$temp = [];
foreach($mission as $k => $v){
echo "####################任务分配开始######################<br />";
echo "--进行第《{$i}》天第《".($k+1)."》个任务《{$v['missionName']}》任务分配:<br />";
$tmp = array(
'missionName' => $v['missionName'],
'personName' => getFamilyName($family,$v)
);
foreach($family as $k_f => $v_f ){
if($v_f['personName']==$tmp['personName']){
$family[$k_f]['personTimes']++;
}
}
$temp[] = $tmp;
echo "任务分配给了{$tmp['personName']}<br />";
echo "####################任务分配结束######################<br />";
echo "<br /><br />";
}
$missionTable[] = $temp;
}
return $missionTable;
}
function getFamilyName($family,$mission){
$tmp = [];
foreach($family as $k => $v){
if($v['personLevel'] >= $mission['missionLevel']){
$tmp[] = $v;
}
}
$person = null;
$person = oderBy($tmp);
return $person[0]['personName'];
}
function oderBy($family){
$sort = array(
'direction' => 'SORT_ASC',
'field' => 'personTimes',
);
$arrSort = array();
foreach($family AS $uniqid => $row){
foreach($row AS $key=>$value){
$arrSort[$key][$uniqid] = $value;
}
}
if($sort['direction']){
array_multisort($arrSort[$sort['field']], constant($sort['direction']), $family);
}
return $family;
}
echo "<pre />";
var_dump(makeMission($data));