简体   繁体   English

在 foreach 循环中增加和重置计数变量

[英]Increment and Reset a count variable inside a foreach loop

I have an multi dimensional array =我有一个多维数组=

array:2 [▼
  1 => array:2 [▼
    0 => array:4 [▼
      "supplier_id" => 1
      "child_product_id" => 54634
      "quantity" => 2
      "shipping_cost" => "4.99"
    ]
    1 => array:4 [▼
      "supplier_id" => 1
      "child_product_id" => 24723
      "quantity" => 1
      "shipping_cost" => "4.99"
    ]
  ]
  2 => array:1 [▼
    0 => array:4 [▼
      "supplier_id" => 2
      "child_product_id" => 19533
      "quantity" => 1
      "shipping_cost" => "18.00"
    ]
  ]
]

What I'd like to do is increment the "quantity" that appears in the array, but only do this per array key我想做的是增加出现在数组中的“数量”,但只对每个数组键执行此操作

eg array key 1 would be 3 and array key 2 would be 1.例如,数组键 1 为 3,数组键 2 为 1。

I need this for some maths I'll be doing further down in the function.我需要这个来做一些数学,我将在 function 中做进一步的事情。

Any suggestions as to how I would do this?关于我将如何做到这一点的任何建议?

I've tried breaking this into a laravel collection, then suming the quantity, but that comes back as 4 when it needs to broken down per supplier, which in this case is the key "1" / "2"我已经尝试将其分解为 laravel 集合,然后将数量相加,但是当它需要按供应商分解时返回为4 ,在这种情况下是关键“1”/“2”

This is the code I have to build the above array.这是我必须构建上述数组的代码。

// Set Some Vars For Splitting By Shipping Cost
        $quantities = array();
        $count = 0;

        foreach ($basket->products as $product)
        {
            // Find Supplier
            $supplier = Supplier::find($product->supplier_id);

            if($supplier->shipping_cost == 0)
            {
                // Split Delivery Amount (FPS / Other)...
                $shipping_cost = $delivery_amount;
            }
            else
            {
                $shipping_cost = $supplier->shipping_cost;
            }

            $quantities[$supplier->id][] = array
            (
                'supplier_id'       => $supplier->id,
                'child_product_id'  => $product->child_product_id,
                'quantity'          => $product->quantity,
                'shipping_cost'     => $shipping_cost,
            );
        }

Would suggest to add an offset qty_sum in each array.建议在每个数组中添加一个偏移量qty_sum

$quantities = array();
$count = 0;

foreach ($basket->products as $product) {
    // Find Supplier
    $supplier = Supplier::find($product->supplier_id);

    if ($supplier->shipping_cost == 0) {
        // Split Delivery Amount (FPS / Other)...
        $shipping_cost = $delivery_amount;
    } else {
        $shipping_cost = $supplier->shipping_cost;
    }

    if (!isset($quantities[$supplier->id])) {
        $quantities[$supplier->id] = [
            'qty_sum' => 0,
            'items'   => [],
        ];
    } else {
        $quantities[$supplier->id]['qty_sum'] += $product->quantity;
        $quantities[$supplier->id]['items'][] = [
            'supplier_id' => $supplier->id,
            'child_product_id' => $product->child_product_id,
            'quantity' => $product->quantity,
            'shipping_cost' => $shipping_cost,
        ];
    }
}

I think it would be a good idea to make two for-loops, since you go through the array that contains the Orders (as arrays) first, and then you need the 2nd for-loop to go through the Array of Orders.我认为创建两个 for 循环是个好主意,因为您首先通过包含订单(作为数组)的数组 go,然后您需要通过订单数组到 go 的第二个 for 循环。

So after the second for-loop is done, you add the count to an array and reset it.因此,在第二个 for 循环完成后,您将计数添加到数组并重置它。

Somewhat like this:有点像这样:

$count = 0;
$qty_array = array();
foreach($basket as $supplier){
    foreach($supplier as $product){
        $count += $product->quantity;
    }
    $qty_array[] = $count;    
    $count = 0;
}

I hope this was useful to you!我希望这对你有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM