Php 每件包裹最大重量为30公斤

Php 每件包裹最大重量为30公斤,php,Php,三个零件的最大总重量为50kg 每件包裹最大重量为30公斤 汽车排气重量为:20KG-总量2=40KG 汽车交流发电机重量为:10KG-总量1=10KG 所以这需要两个包裹 包裹最大重量为30kg包裹1-汽车排气和交流发电机(第一个包裹需要1个排气20KG和1个交流发电机10KG才能制造30kg) 包裹最大重量为30kg包裹2-汽车排气(第二个包裹的排气量为20KG) 像这样展示: 包裹编号| |零件名称| |零件数量| |重量 1 | |汽车排气| | 1 | | 20KG 1 | | |

三个零件的最大总重量为50kg

每件包裹最大重量为30公斤

汽车排气重量为:20KG-总量2=40KG

汽车交流发电机重量为:10KG-总量1=10KG

所以这需要两个包裹

  • 包裹最大重量为30kg包裹1-汽车排气和交流发电机(第一个包裹需要1个排气20KG和1个交流发电机10KG才能制造30kg)

  • 包裹最大重量为30kg包裹2-汽车排气(第二个包裹的排气量为20KG)

像这样展示:

包裹编号| |零件名称| |零件数量| |重量

1 | |汽车排气| | 1 | | 20KG

1 | | | | 1 | | 10KG交流发电机

2 | |汽车排气| | 1 | | 20KG


需要像这样显示php脚本吗

(注释/答案中的代码)

$nums=array(20,20,10);
排序($nums);
$i=0;
而($i=sizeof($nums)){
打破
}
回声“
”; $i=$i+1; }
这应该行得通,您必须尝试一下以确保,但是代码中有一些注释,可以告诉您它是如何工作的

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$parts = [ [ 'desc' => 'Exhaust', 'weight' =>20],
    ['desc' => 'Car Exhaust2', 'weight' => 20],
    ['desc' => 'Alternator', 'weight' => 10]
];
$maxWeight = 30;
$parcels = [];

// Create first parcel
$parcels[] = ['weight' => 0, 'items' => []];
foreach ( $parts as $item ) {
    $added = false;
    // Look through all existing parcels
    foreach ( $parcels as &$parcel ) {
        // If there is enough space for this item - add to parcel
        if ( $parcel['weight'] + $item['weight'] <= $maxWeight ) {
            $parcel['weight'] += $item['weight'];
            $parcel['items'][] = $item;
            $added = true;
            break;
        }
    }
    // If there wasn't enough space in existing parcels, create a new one
    if ( $added == false )  {
        $parcels[] = ['weight' => $item['weight'], 'items' => [$item]];
    }
}

print_r($parcels);

需要像这样显示php脚本。请添加到目前为止的任何代码,我们可以帮助解决此代码的特定问题,但我并不总是想为您完成工作。$nums=array(20,20,10);排序($nums);$i=0;而($i=sizeof($nums)){break;}echo“
”;$i=$i+1;如果这已经回答了你的问题,请考虑把它标记为答案。()
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$parts = [ [ 'desc' => 'Exhaust', 'weight' =>20],
    ['desc' => 'Car Exhaust2', 'weight' => 20],
    ['desc' => 'Alternator', 'weight' => 10]
];
$maxWeight = 30;
$parcels = [];

// Create first parcel
$parcels[] = ['weight' => 0, 'items' => []];
foreach ( $parts as $item ) {
    $added = false;
    // Look through all existing parcels
    foreach ( $parcels as &$parcel ) {
        // If there is enough space for this item - add to parcel
        if ( $parcel['weight'] + $item['weight'] <= $maxWeight ) {
            $parcel['weight'] += $item['weight'];
            $parcel['items'][] = $item;
            $added = true;
            break;
        }
    }
    // If there wasn't enough space in existing parcels, create a new one
    if ( $added == false )  {
        $parcels[] = ['weight' => $item['weight'], 'items' => [$item]];
    }
}

print_r($parcels);