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

根据数量划分物品

根据数量划分物品

PHP
ABOUTYOU 2023-08-19 14:27:57
我们有一个自定义调度系统,其中有订单,如果订单中的总数量超过 2,那么我们需要将内容拆分为两个订单。那么例如...订单包含:物品A x2物品 B x2我需要将项目 B 移至第二个订单。另一个例子是:物品 A x4我需要将 2 个项目移至第二个订单,因此我在一个输出中留下项目 A x2,在另一个输出中保留相同的项目。我正在使用以下内容循环遍历订单中的商品和数量。$total_products = array();foreach($products as $product){  $product_id = $product['sellable']['id'];  $product_quantity = $product['quantity'];  $product_price = $product['price_per_unit'];  array_push($total_products, array($product_id, $product_quantity, $product_price));}我怎样才能按该顺序总计项目 - 一旦任何数量达到 2,我可以将它们移动到第二个数组吗?使用多个数组是最好的方法吗?这是已生成的数组的示例(可以是变量):Array(    [0] => Array        (            [0] => 39235995            [1] => 3            [2] => 2.81        )    [1] => Array        (            [0] => 39236029            [1] => 1            [2] => 2.952        )    [2] => Array        (            [0] => 39236015            [1] => 1            [2] => 3.333        )    [3] => Array        (            [0] => 39235997            [1] => 1            [2] => 2.667        ))
查看完整描述

3 回答

?
浮云间

TA贡献1829条经验 获得超4个赞

我假设它$product_quantity始终是一个正整数。但也许您想在继续之前检查一下,如下所示:


$product_quantity = $product['quantity']; // this is already in your code

if (!is_int($product_quantity) || $product_quantity < 1) {

    // handle the invalid data

}

现在我们确定只处理正整数,我们可以在每次迭代中检查是否没有超过每个订单允许的商品数量:


$total_products = array();


$product_per_order = 0;


foreach($products as $product){


  $product_id = $product['sellable']['id'];

  $product_quantity = $product['quantity'];

  $product_price = $product['price_per_unit'];


  while ($product_quantity > 2) {

    // handle delivery with 2 products of type $product_id

    $product_quantity -= 2;

  }


  if ($product_quantity <= 0) { // We delivered all of what was ordered

    continue;

  }


  $product_per_order += $product_quantity;


  if ($product_per_order > 2) {

     // handle more deliveries

     $product_per_order = 0; // initialize

  }


  $total_products[] = array($product_id, $product_quantity, $product_price);

}


这只是跟踪产品数量的方法的概述,您应该改进它并根据自己的需求进行自我定制。


查看完整回答
反对 回复 2023-08-19
?
森栏

TA贡献1810条经验 获得超5个赞

设法通过使用以下内容来解决这个问题,这给了我我需要的单独的包裹:


// Add the product info we need into a new array

$total_products = array();

foreach($products as $product){


  $product_id = $product['sellable']['id'];

  $product_quantity = $product['quantity'];

  $product_price = $product['price_per_unit'];


  $total_products[] = array('id' => $product_id, 'quantity' => $product_quantity, 'price' => $product_price);

}


// Separate out the quantities for each product, one per array

$resultArr = [];

foreach($total_products as $item){

  for($i = 0; $i < $item['quantity']; $i++){

      $resultArr[] = array(

        'id' => $item['id'],

        'quantity' => 1,

        'price' => $item['price'],

      );

  }

}


// Divide up into the correct amount of parcels

$parcel_size = 2;

$parcels = array_chunk($resultArr, $parcel_size);


查看完整回答
反对 回复 2023-08-19
?
茅侃侃

TA贡献1842条经验 获得超21个赞

我相信这满足了将产品拆分为多个订单的要求,但仍保留每个订单中的产品数据:


$orders = [];

$i = 0;

foreach ($products as $product) {

    foreach (array_fill(0, ceil($product['quantity'] / 2), $product) as $splitProduct) {

        $splitProduct['quantity'] = min(2, $product['quantity']);

        $orders[$i][] = $splitProduct;


        $total = array_reduce($orders[$i], function ($carry, $product) {

            return $carry + $product['quantity'];

        }, 0);


        if ($total >= 2) {

            $i++;

        }

    }

}

示例1:


输入:


$products[] = ['sellable' => ['id' => 1], 'quantity' => 4, 'price_per_unit' => 0];

输出:


Array

(

    [0] => Array

        (

            [0] => Array

                (

                    [sellable] => Array

                        (

                            [id] => 1

                        )


                    [quantity] => 2

                    [price_per_unit] => 0

                )


        )


    [1] => Array

        (

            [0] => Array

                (

                    [sellable] => Array

                        (

                            [id] => 1

                        )


                    [quantity] => 2

                    [price_per_unit] => 0

                )


        )


)

示例2:


输入:


$products[] = ['sellable' => ['id' => 1], 'quantity' => 2, 'price_per_unit' => 0];

$products[] = ['sellable' => ['id' => 2], 'quantity' => 2, 'price_per_unit' => 0];

输出:


Array

(

    [0] => Array

        (

            [0] => Array

                (

                    [sellable] => Array

                        (

                            [id] => 1

                        )


                    [quantity] => 2

                    [price_per_unit] => 0

                )


        )


    [1] => Array

        (

            [0] => Array

                (

                    [sellable] => Array

                        (

                            [id] => 2

                        )


                    [quantity] => 2

                    [price_per_unit] => 0

                )


        )


)


查看完整回答
反对 回复 2023-08-19
  • 3 回答
  • 0 关注
  • 72 浏览

添加回答

举报

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