第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

根據(jù)數(shù)量劃分物品

根據(jù)數(shù)量劃分物品

PHP
ABOUTYOU 2023-08-19 14:27:57
我們有一個(gè)自定義調(diào)度系統(tǒng),其中有訂單,如果訂單中的總數(shù)量超過 2,那么我們需要將內(nèi)容拆分為兩個(gè)訂單。那么例如...訂單包含:物品A x2物品 B x2我需要將項(xiàng)目 B 移至第二個(gè)訂單。另一個(gè)例子是:物品 A x4我需要將 2 個(gè)項(xiàng)目移至第二個(gè)訂單,因此我在一個(gè)輸出中留下項(xiàng)目 A x2,在另一個(gè)輸出中保留相同的項(xiàng)目。我正在使用以下內(nèi)容循環(huán)遍歷訂單中的商品和數(shù)量。$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));}我怎樣才能按該順序總計(jì)項(xiàng)目 - 一旦任何數(shù)量達(dá)到 2,我可以將它們移動(dòng)到第二個(gè)數(shù)組嗎?使用多個(gè)數(shù)組是最好的方法嗎?這是已生成的數(shù)組的示例(可以是變量):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貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個(gè)贊

我假設(shè)它$product_quantity始終是一個(gè)正整數(shù)。但也許您想在繼續(xù)之前檢查一下,如下所示:


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

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

    // handle the invalid data

}

現(xiàn)在我們確定只處理正整數(shù),我們可以在每次迭代中檢查是否沒有超過每個(gè)訂單允許的商品數(shù)量:


$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);

}


這只是跟蹤產(chǎn)品數(shù)量的方法的概述,您應(yīng)該改進(jìn)它并根據(jù)自己的需求進(jìn)行自我定制。


查看完整回答
反對(duì) 回復(fù) 2023-08-19
?
森欄

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊

設(shè)法通過使用以下內(nèi)容來解決這個(gè)問題,這給了我我需要的單獨(dú)的包裹:


// 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);


查看完整回答
反對(duì) 回復(fù) 2023-08-19
?
茅侃侃

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超22個(gè)贊

我相信這滿足了將產(chǎn)品拆分為多個(gè)訂單的要求,但仍保留每個(gè)訂單中的產(chǎn)品數(shù)據(jù):


$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

                )


        )


)


查看完整回答
反對(duì) 回復(fù) 2023-08-19
  • 3 回答
  • 0 關(guān)注
  • 156 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)