doujia2386 2016-12-08 11:58
浏览 27
已采纳

php脚本生成计划

I want to distribute some personnel in a week every day there are 3 times 7h-15h 15-23h 23h-7h .

The problem that I don't want a person to show more than one time in a day. I want to distribute personnel list to the day of week I try this:

<?php
$input = array("Name1","Name2",  "Name3","Name4");
$rand_keys = array_rand($input, 3);


?>
<table  border=1>

<tr>

<th> Samedi </th> <th> Dimanche </th> <th> Lundi </th> <th> Mardi </th> <th> Merecredi </th> <th> Jeudi </th> <th> Vendredi </th>
</tr>
<tr>
<?php
$j=1;
for($i=0;$i<7;$i++){

    echo "<td>";
    for($k=0;$k<3;$k++){

        echo  $input[$rand_keys[0]] ."7H-15H
" ;   
        echo  $input[$rand_keys[1]] ."15H-23H
" ;  
        echo  $input[$rand_keys[2]] ."23H-7H
" ;           

    }
    echo "</td>";
}   
?>
  • 写回答

1条回答 默认 最新

  • duanbei1903 2016-12-08 13:11
    关注

    You don't need the second loop for ($k = 0; $k < 3; $k++), because you're already showing each shift with the 3 echo statements.

    for($i=0;$i<7;$i++){
    
        echo "<td>
    ";
    
        echo  $input[$rand_keys[0]] ." 7H-15H
    " ;   
        echo  $input[$rand_keys[1]] ." 15H-23H
    " ;  
        echo  $input[$rand_keys[2]] ." 23H-7H
    " ;           
    
        echo "</td>
    ";
    }
    

    You're also using the same schedule every day of the week. So one of the people is never being scheduled for the entire week. You probably should shuffle the array each day, not just at the beginning of the script.

    for($i=0;$i<7;$i++){
    
        echo "<td>
    ";
        shuffle($input);
        echo  $input[0] ." 7H-15H
    " ;   
        echo  $input[1] ." 15H-23H
    " ;  
        echo  $input[2] ." 23H-7H
    " ;           
    
        echo "</td>
    ";
    }
    

    DEMO

    To keep a person from working two shifts in a row if they're assigned to 23H-7H and then 7H-15H the next day, you can set a variable to that person, and check if the shuffle put them first.

    $last_shift = $input[array_rand($input)];
    
    for($i=0;$i<7;$i++){
    
        echo "<td>
    ";
        shuffle($input);
        while ($input[0] == $last_shift) {
            shuffle($input);
        }
        echo  $input[0] ." 7H-15H
    " ;   
        echo  $input[1] ." 15H-23H
    " ;  
        echo  $input[2] ." 23H-7H
    " ;        
    
        $last_shift = $input[2];
        echo "</td>
    ";
    }
    

    DEMO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历