猿问

根据数量划分物品

我们有一个自定义调度系统,其中有订单,如果订单中的总数量超过 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

        )


)


ABOUTYOU
浏览 115回答 3
3回答

浮云间

我假设它$product_quantity始终是一个正整数。但也许您想在继续之前检查一下,如下所示:$product_quantity = $product['quantity']; // this is already in your codeif (!is_int($product_quantity) || $product_quantity < 1) {&nbsp; &nbsp; // handle the invalid data}现在我们确定只处理正整数,我们可以在每次迭代中检查是否没有超过每个订单允许的商品数量:$total_products = array();$product_per_order = 0;foreach($products as $product){&nbsp; $product_id = $product['sellable']['id'];&nbsp; $product_quantity = $product['quantity'];&nbsp; $product_price = $product['price_per_unit'];&nbsp; while ($product_quantity > 2) {&nbsp; &nbsp; // handle delivery with 2 products of type $product_id&nbsp; &nbsp; $product_quantity -= 2;&nbsp; }&nbsp; if ($product_quantity <= 0) { // We delivered all of what was ordered&nbsp; &nbsp; continue;&nbsp; }&nbsp; $product_per_order += $product_quantity;&nbsp; if ($product_per_order > 2) {&nbsp; &nbsp; &nbsp;// handle more deliveries&nbsp; &nbsp; &nbsp;$product_per_order = 0; // initialize&nbsp; }&nbsp; $total_products[] = array($product_id, $product_quantity, $product_price);}这只是跟踪产品数量的方法的概述,您应该改进它并根据自己的需求进行自我定制。

森栏

设法通过使用以下内容来解决这个问题,这给了我我需要的单独的包裹:// Add the product info we need into a new array$total_products = array();foreach($products as $product){&nbsp; $product_id = $product['sellable']['id'];&nbsp; $product_quantity = $product['quantity'];&nbsp; $product_price = $product['price_per_unit'];&nbsp; $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){&nbsp; for($i = 0; $i < $item['quantity']; $i++){&nbsp; &nbsp; &nbsp; $resultArr[] = array(&nbsp; &nbsp; &nbsp; &nbsp; 'id' => $item['id'],&nbsp; &nbsp; &nbsp; &nbsp; 'quantity' => 1,&nbsp; &nbsp; &nbsp; &nbsp; 'price' => $item['price'],&nbsp; &nbsp; &nbsp; );&nbsp; }}// Divide up into the correct amount of parcels$parcel_size = 2;$parcels = array_chunk($resultArr, $parcel_size);

茅侃侃

我相信这满足了将产品拆分为多个订单的要求,但仍保留每个订单中的产品数据:$orders = [];$i = 0;foreach ($products as $product) {&nbsp; &nbsp; foreach (array_fill(0, ceil($product['quantity'] / 2), $product) as $splitProduct) {&nbsp; &nbsp; &nbsp; &nbsp; $splitProduct['quantity'] = min(2, $product['quantity']);&nbsp; &nbsp; &nbsp; &nbsp; $orders[$i][] = $splitProduct;&nbsp; &nbsp; &nbsp; &nbsp; $total = array_reduce($orders[$i], function ($carry, $product) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $carry + $product['quantity'];&nbsp; &nbsp; &nbsp; &nbsp; }, 0);&nbsp; &nbsp; &nbsp; &nbsp; if ($total >= 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}示例1:输入:$products[] = ['sellable' => ['id' => 1], 'quantity' => 4, 'price_per_unit' => 0];输出:Array(&nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [sellable] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [quantity] => 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [price_per_unit] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [sellable] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [quantity] => 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [price_per_unit] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; ))示例2:输入:$products[] = ['sellable' => ['id' => 1], 'quantity' => 2, 'price_per_unit' => 0];$products[] = ['sellable' => ['id' => 2], 'quantity' => 2, 'price_per_unit' => 0];输出:Array(&nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [sellable] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [quantity] => 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [price_per_unit] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [sellable] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [quantity] => 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [price_per_unit] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; ))
随时随地看视频慕课网APP
我要回答