Home > OS >  Increment and Reset a count variable inside a foreach loop
Increment and Reset a count variable inside a foreach loop

Time:11-20

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

e.g array key 1 would be 3 and array key 2 would be 1.

I need this for some maths I'll be doing further down in the 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"

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,
            );
        }

CodePudding user response:

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.

So after the second for-loop is done, you add the count to an array and reset it.

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!

CodePudding user response:

Would suggest to add an offset qty_sum in each array.

$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,
        ];
    }
}
  • Related