Php 按数量划分项目

Php 按数量划分项目,php,Php,我们有一个定制的调度系统,我们有订单,如果订单中的总数量超过2,那么我们需要将内容分为两个订单。比如说 订单包括: 项目A x2 项目B x2 我需要把B项移到第二个订单 另一个例子是: 项目A x4 我需要将两个项目移动到第二个顺序,这样我在一个输出中留下一个x2项目,在另一个输出中留下相同的项目 我使用下面的代码循环查看订单中的项目和数量 $total_products = array(); foreach($products as $product){ $product_id

我们有一个定制的调度系统,我们有订单,如果订单中的总数量超过2,那么我们需要将内容分为两个订单。比如说

订单包括:

  • 项目A x2
  • 项目B x2
我需要把B项移到第二个订单

另一个例子是:

  • 项目A x4
我需要将两个项目移动到第二个顺序,这样我在一个输出中留下一个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,我可以将它们移动到第二个数组中吗?使用多个阵列是最好的方法吗

这是已生成的数组示例(可以是变量):


我假设
$product\u quantity
始终是一个正整数。但在继续之前,您可能想检查一下,比如:

$product\u quantity=$product['quantity'];//这已在您的代码中
如果(!is_int($product_quantity)| |$product_quantity<1){
//处理无效数据
}
现在我们确定只处理正整数,我们可以在每次迭代中检查是否不超过每个订单允许的项数:

$total_products=array();
$product\u/订单=0;
foreach($products as$product){
$product_id=$product['SALLABLE']['id'];
$product_quantity=$product['quantity'];
$product_price=$product['每单位价格'];
而($product\U QUOTE>2){
//使用$product\U id类型的2个产品处理交付
$product_数量-=2;
}
如果($product\U数量2){
//处理更多的交货
$product\u per\u order=0;//初始化
}
$total_products[]=数组($product_id,$product_quantity,$product_price);
}

这只是一个跟踪产品数量的方法大纲,您应该改进它并根据自己的需要进行自我调整。

我假设
$product\u quantity
始终是一个正整数。但在继续之前,您可能想检查一下,比如:

$product\u quantity=$product['quantity'];//这已在您的代码中
如果(!is_int($product_quantity)| |$product_quantity<1){
//处理无效数据
}
现在我们确定只处理正整数,我们可以在每次迭代中检查是否不超过每个订单允许的项数:

$total_products=array();
$product\u/订单=0;
foreach($products as$product){
$product_id=$product['SALLABLE']['id'];
$product_quantity=$product['quantity'];
$product_price=$product['每单位价格'];
而($product\U QUOTE>2){
//使用$product\U id类型的2个产品处理交付
$product_数量-=2;
}
如果($product\U数量2){
//处理更多的交货
$product\u per\u order=0;//初始化
}
$total_products[]=数组($product_id,$product_quantity,$product_price);
}

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

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

$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];
$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] => 1
                        )

                    [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
                )

        )

)
例2:

输入:

$products[] = ['sellable' => ['id' => 1], 'quantity' => 4, 'price_per_unit' => 0];
$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] => 1
                        )

                    [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
                )

        )

)

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

$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];
$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] => 1
                        )

                    [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
                )

        )

)
例2:

输入:

$products[] = ['sellable' => ['id' => 1], 'quantity' => 4, 'price_per_unit' => 0];
$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] => 1
                        )

                    [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
                )

        )

)

通过使用以下方法,我成功地解决了这一问题,这为我提供了所需的单独包裹:

// 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);
//将我们需要的产品信息添加到新数组中
$total_products=array();
foreach($products as$product){
$product_id=$product['SALLABLE']['id'];
$product_quantity=$product['quantity'];
$product_price=$product['每单位价格'];
$total\u products[]=array('id'=>$product\u id,'quantity'=>$product\u quantity,'price'=>$product\u price);
}
//将每个产品的数量分开,每个阵列一个
$resultar=[];
foreach($商品总计){
对于($i=0;$i<$item['quantity'];$i++){
$resultArr[]=数组(
'id'=>$item['id'],
“数量”=>1,
“价格”=>$item[“价格”],
);
}
}
//分成正确数量的包裹
$parcel_size=2;
$parcels=数组块($resultArr,$parcel\U大小);

通过使用以下方法设法解决此问题,这将为我提供所需的单独包裹:

// 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);
//将我们需要的产品信息添加到新数组中
$total_products=array();
foreach($products as$product){
$product_id=$product['SALLABLE']['id'];
$product_quantity=$product['quantity'];
$product_price=$product['每单位价格'];
$total\u products[]=array('id'=>$product\u id,'quantity'=>$product\u quantity,'price'=>$product\u price);
}
//将每个产品的数量分开,每个阵列一个
$resultar=[];
foreach($商品总计){
对于($i=0;$i<$item['quantity'];$i++){
$resultArr[]=数组(
'id'=>$item['id'],
“数量”=>1,
“价格”=>$item[“价格”],
);
}
}
//分成正确数量的包裹
$parcel_size=2;
$parcels=数组块($resultArr,$parcel\U大小);

为什么您使用
array\u push()
而不是
$total\u products[]=…
?为什么您使用
array\u push()
而不是
$total\u products[]=…
?感谢您的回答,这似乎很接近,但我可能解释得不够。包裹需要分为不超过2个的总数量。因此,这可以是多种产品的混合,也可以是所有相同的产品,但不超过一个包裹中两种产品的总数量。我刚刚试过A项x3、B项x1、C项x1和D项x1(总共6个)。它创建了一个数组wi