猿问

在 PHP foreach 循环中管理计划

我有如下表

所以有优先级和平方英尺,我想开始制造计划(从当前日期开始),每天将处理 1700 平方英尺,如下图所示

https://img.mukewang.com/650d35e800010eb906540254.jpg

但正如你所看到的,它不起作用


我试过下面的代码


foreach($priorityArraySum as $key=>$val)

{

                    

   $totalDays=ceil($val/1700);

   $cutSQFT=$val;

                    

    for($j=1;$j<=$totalDays;$j++)

    {

        if($cutSQFT>1700)

        {

          echo '1700';

         $cutDate=date('Y-m-d', strtotime($cutDate. ' + 1 days'));

         $cutSQFT=$cutSQFT-1700;

        }

        else

        {

          echo $cutSQFT;

          $cutSQFT=$cutSQFT-$cutSQFT;

        }

    }

}


@Nigel,为了使这种动态化,我已将代码更改为下面的代码,但它不起作用。


$pln_qry=mysql_query("select * from tbl_mfg_schedule where ms_date='".$today."'") or die(mysql_error());

                $pln_data=mysql_fetch_array($pln_qry);

                

                $max = $pln_data['ms_po_sqft'];

                $dailyLeft = $max;

                $current = reset($priorityArraySum);

                $output = [];

                //$day = date('Y-m-d');

                $day = date('Y-m-d');

                while (true)    {

                    

                    $pln_qry=mysql_query("select * from tbl_mfg_schedule where ms_date='".$today."'") or die(mysql_error());

                    $pln_data=mysql_fetch_array($pln_qry);

                

                    $max = $pln_data['ms_po_sqft'];

                

                        if ( $current >= $dailyLeft )   {

                            //$day=date('Y-m-d', strtotime($day. ' + 1 days'));

                            $output[] = ["priority" => key($priorityArraySum),

                                    "amount" => $dailyLeft,

                                    "day" => $day

                            ];

                            $day=date('Y-m-d', strtotime($day. ' + 1 days'));

                            $current -= $dailyLeft;

                            $dailyLeft = $max;

                        }


长风秋雁
浏览 81回答 1
1回答

UYOU

此代码使用while()循环创建工作天数的输出数组。它用于$current跟踪每个项目以及剩余的分配量。它还用于$dailyLeft跟踪特定日期剩余的容量。它会检查两者,如果当前小于剩余的每日容量,则会为该项目分配一整天并重置每日容量。如果当天有额外容量,则会将此项目分配给该天并获取下一个项目。$day++仅当当天的容量已满时,该天才会增加(使用)...$max = 1700;$dailyLeft = $max;$current = reset($priorityArraySum);$output = [];$day = 1;while (true)&nbsp; &nbsp; {&nbsp; &nbsp; // echo $current."/".$dailyLeft."=".$day.PHP_EOL;&nbsp; &nbsp; if ( $current >= $dailyLeft )&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; $output[] = ["priority" => key($priorityArraySum),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "amount" => $dailyLeft,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "day" => $day++&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; $current -= $dailyLeft;&nbsp; &nbsp; &nbsp; &nbsp; $dailyLeft = $max;&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $output[] = ["priority" => key($priorityArraySum),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "amount" => $current,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "day" => $day&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; $dailyLeft -= $current;&nbsp; &nbsp; &nbsp; &nbsp; if ( ($current = next($priorityArraySum)) === false )&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}print_r($output);
随时随地看视频慕课网APP
我要回答