为了账号安全,请及时绑定邮箱和手机立即绑定

需要在此循环中将常量更改为动态

需要在此循环中将常量更改为动态

PHP
摇曳的蔷薇 2023-09-22 16:31:40
我正在运行制造计划(使用 PHP 代码),但目前它是静态的(意味着我无法添加假期或无法更改制造能力 - 当前制造能力是静态的每天 1700 平方英尺)请看下面的代码$max = 1700;$dailyLeft = $max;$current = reset($priorityArraySum);$output = [];//$day = date('Y-m-d');$day = date('Y-m-d');while (true)    {  // echo $current."/".$dailyLeft."=".$day.PHP_EOL;  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;  } else {    $output[] = ["priority" => key($priorityArraySum), "amount" => $current, "day" => $day];    $dailyLeft -= $current;    if ( ($current = next($priorityArraySum)) === false ) {      break;    }  }}echo '<pre/>';print_r($output);echo '<pre/>';exit;使用上面的代码,我可以安排我的制造计划,见下图当前日程图像当前代码的问题是,我们每天有 1700 个静态容量,我们希望有动态容量,例如第一天 1700 个,第二天 1900 个,节假日 0 个。我们如何更改此代码以使其动态化?目前我正在尝试下面的代码,但它不起作用
查看完整描述

1 回答

?
白猪掌柜的

TA贡献1893条经验 获得超10个赞

这是基于您显示的第二个代码。if()一个问题是,您总是将这一天推迟,并且您应该只在当天容量已满的第一部分执行此操作。


为了方便起见,我还在循环中预先检索了所有日期和容量,并将它们存储在以日期为键的数组中。


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

$pln_qry=mysql_query("select * 

    from tbl_mfg_schedule 

    where ms_date > '".$today."' 

    order by ms_date") or die(mysql_error());

// Load list of days and production

$plnList = [];

while ( $pln_data=mysql_fetch_array($pln_qry) ) {

    $plnList [ $pln_data['ms_date'] ] = $pln_data['ms_po_sqft'];

}



$current = reset($priorityArraySum);

$output = [];

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

// Set max from production plan

$max = $plnList[ $day ];

//$max = 1700;

$dailyLeft = $max;

while (true)    {

    //echo $dailyLeft."</br>";

    

    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;

        

        // Move onto the next days capacity, if no specific value assume

        // same as last ( ?? $max)

        $max = $plnList[ $day ] ?? $max;

        $dailyLeft = $max;

        

    }

    else    {

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

                "amount" => $current,

                "day" => $day

        ];

        $dailyLeft -= $current;

        if ( ($current = next($priorityArraySum)) === false )   {

            break;

        }

    }

    

}

echo '<pre/>';

print_r($output);

echo '<pre/>';

使用一些虚拟测试数据(请注意,缺少日期,因此假设它将与...2020-08-14相同2020-08-13


$priorityArraySum = [2000, 200, 6000, 1200];


$plnList = [ '2020-08-10' => 1700,

        '2020-08-11' => 1700,

        '2020-08-12' => 0,

        '2020-08-13' => 500,

        '2020-08-15' => 1700];

它产生...


<pre/>Array

(

    [0] => Array

        (

            [priority] => 0

            [amount] => 1700

            [day] => 2020-08-10

        )


    [1] => Array

        (

            [priority] => 0

            [amount] => 300

            [day] => 2020-08-11

        )


    [2] => Array

        (

            [priority] => 1

            [amount] => 200

            [day] => 2020-08-11

        )


    [3] => Array

        (

            [priority] => 2

            [amount] => 1200

            [day] => 2020-08-11

        )


    [4] => Array

        (

            [priority] => 2

            [amount] => 0

            [day] => 2020-08-12

        )


    [5] => Array

        (

            [priority] => 2

            [amount] => 500

            [day] => 2020-08-13

        )


    [6] => Array

        (

            [priority] => 2

            [amount] => 500

            [day] => 2020-08-14

        )


    [7] => Array

        (

            [priority] => 2

            [amount] => 1700

            [day] => 2020-08-15

        )


    [8] => Array

        (

            [priority] => 2

            [amount] => 1700

            [day] => 2020-08-16

        )


    [9] => Array

        (

            [priority] => 2

            [amount] => 400

            [day] => 2020-08-17

        )


    [10] => Array

        (

            [priority] => 3

            [amount] => 1200

            [day] => 2020-08-17

        )


)


查看完整回答
反对 回复 2023-09-22
  • 1 回答
  • 0 关注
  • 52 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信