如何在未达到指定值之前对数组项的值求和? [英] How to do sum of array item's value until not reached at specified value?

查看:63
本文介绍了如何在未达到指定值之前对数组项的值求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对数组项的值求和,直到未达到指定值。

I want to do sum of array item's value until not reached at specified value.

让我详细解释。

我希望每个框中的值 20 k.g ,所以下面是我的数组。

I want 20 k.g value in each box , So following is my array.

Array
(
    [0] => Array
        (
            [product_id] => 2
            [customer_id] => 1820
            [order_id] => M-AAH-959
            [quantity] => 5
            [weight] => 1.3
            [cubic] => 0.00267
            [total_weight] => 6.5
            [total_cubic] => 0.00267
        )

    [1] => Array
        (
            [product_id] => 3
            [customer_id] => 1820
            [order_id] => M-AAH-959
            [quantity] => 6
            [weight] => 1.5
            [cubic] => 0.00348
            [total_weight] => 9
            [total_cubic] => 0.00348
        )

    [2] => Array
        (
            [product_id] => 8
            [customer_id] => 1820
            [order_id] => M-AAH-959
            [quantity] => 7
            [weight] => 1.5
            [cubic] => 0.00267
            [total_weight] => 10.5
            [total_cubic] => 0.00267
        )

)

您可以在上面的数组中看到有3个字段数量,重量& total_weight

You can see in above array there are 3 fields quantity,weight & total_weight.

因此在循环期间,我想将 20 的值设置为每个项目的重量总和。我们可以取数量&

So during loop i want to set 20 value to single box from sum of each items weight. we can take quantity & weight from any three items for set 20 value for each.





So output will be..

Array (

     [0] => Array('box_item'=>20)
     [1] => Array('box_item'=>6)

)

让我解释一下输出数组。.

Let me explain output array..

您可以看到第一个数组 6.5 &第二个 9 &第三个 10.5

You can see total_weigh of First array 6.5 & second 9 & third 10.5

我想为每个值设置 20 框,因此计算将为

I want to set 20 value per box, So calculation will be

6.5 + 9 = 15.5,然后从第三个数组中添加 4.5 $ c> 1.5 * 3 = 4.5

6.5 + 9 = 15.5 , then add 4.5 from third array like 1.5*3 = 4.5

第一个框用于 20 &剩余重量值为 6 ,因此将其设置为第二个盒子。

So first box is for 20 & remain weight value is 6 so it'll be set is for second box.

注意:目前,我仅以3件物品为例,但是这里会有更多物品,我必须为每件物品的重量设置20公斤的价值。

Note: Currently i have taken only 3 items for example purpose but there will be more items here and I have to set 20 k.g value for each for from weight of items value.

我使用了以下代码,但没有帮助。此代码未提供正确的输出

I have used following code but not helpful. This code is not given proper output

// First calculate the total
foreach ($main as $key => $package) {
    $packageInfo[$key]['total_weight'] = $package['quantity'] * $package['weight'];

}

// Then check count the packages ?
    $packages = [];
    $packageTotalWeight = 0;
    $packageItemWeight = 0;
    $cubicTotal = 0;

    foreach ($packageInfo as $key => $package) {
        if(($packageTotalWeight + $package['total_weight']) > 20){
            $packages[]['final_total'] = $packageTotalWeight;
            $packageTotalWeight = $package['total_weight'];       
        } else {            
            $packageTotalWeight += $package['total_weight'];            
        }

    }


推荐答案

此功能将为您包装盒子,依次从每种产品中选择,直到盒子达到最大盒子重量,然后开始新的盒子:

This function will "pack" the boxes for you, selecting from each product in turn until a box reaches the maximum box weight, then starting a new one:

function pack_boxes($products, $box_weight) {
    $boxes = array();
    $this_box_weight = 0;
    $box_number = 0;
    foreach ($products as $product) {
        // will all this product fit?
        if ($product['total_weight'] < $box_weight - $this_box_weight) {
            // yes - add this product to the box
            $boxes[$box_number][] = $product;
            $this_box_weight += $product['total_weight'];
            $boxes[$box_number]['box_weight'] = $this_box_weight;
        }
        else {
            // no - add the part that will fit to this box
            $num_products = floor(($box_weight - $this_box_weight) / $product['weight']);
            $balance = $product['quantity'] - $num_products;
            $product['quantity'] = $num_products;
            $product['total_weight'] = $num_products * $product['weight'];
            $boxes[$box_number][] = $product;
            $boxes[$box_number]['box_weight'] = $this_box_weight + $num_products * $product['weight'];
            // add the balance to a new box
            $box_number++;
            $product['quantity'] = $balance;
            $this_box_weight = $product['total_weight'] = $balance * $product['weight'];
            $boxes[$box_number][] = $product;
            $boxes[$box_number]['box_weight'] = $this_box_weight;
        }
    }
    return $boxes;
}

每个框中的总重量以 $表示box [*] ['box_weight']

样品用量(12是每个盒子的最大重量)以获取每个盒子的盒子重量:

Sample usage (12 is maximum weight per box) to get the box weights per box:

$boxes = pack_boxes($products, 12);
print_r(array_column($boxes, 'box_weight'));

输出(仅包装箱重量):

Output (box weights only):

Array
(
    [0] => 11
    [1] => 12
    [2] => 3
)

或完整数据结果:

print_r($boxes);

输出

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [product_id] => 2
                    [customer_id] => 1820
                    [order_id] => M-AAH-959
                    [quantity] => 5
                    [weight] => 1.3
                    [cubic] => 0.00267
                    [total_weight] => 6.5
                    [total_cubic] => 0.00267
                )    
            [box_weight] => 11
            [1] => Array
                (
                    [product_id] => 3
                    [customer_id] => 1820
                    [order_id] => M-AAH-959
                    [quantity] => 3
                    [weight] => 1.5
                    [cubic] => 0.00348
                    [total_weight] => 4.5
                    [total_cubic] => 0.00348
                )    
        )    
    [1] => Array
        (
            [0] => Array
                (
                    [product_id] => 3
                    [customer_id] => 1820
                    [order_id] => M-AAH-959
                    [quantity] => 3
                    [weight] => 1.5
                    [cubic] => 0.00348
                    [total_weight] => 4.5
                    [total_cubic] => 0.00348
                )    
            [box_weight] => 12
            [1] => Array
                (
                    [product_id] => 8
                    [customer_id] => 1820
                    [order_id] => M-AAH-959
                    [quantity] => 5
                    [weight] => 1.5
                    [cubic] => 0.00267
                    [total_weight] => 7.5
                    [total_cubic] => 0.00267
                )    
        )    
    [2] => Array
        (
            [0] => Array
                (
                    [product_id] => 8
                    [customer_id] => 1820
                    [order_id] => M-AAH-959
                    [quantity] => 2
                    [weight] => 1.5
                    [cubic] => 0.00267
                    [total_weight] => 3
                    [total_cubic] => 0.00267
                )    
            [box_weight] => 3
        )    
)

在3v4l.org上进行演示

这篇关于如何在未达到指定值之前对数组项的值求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆