megha
megha

Reputation: 80

how to add the integer values using foreach in php

I just want to know the syntax of adding the quantity using foreach. Any help is greatly appreciable.

Here is the code what I do so far:

$qproducts = '0';
foreach ($this->cart->getProducts() as $product) {  
    $qproducts .= $product['quantity'];
}       
$this->data['pquantity'] = $qproducts;

Upvotes: 3

Views: 9442

Answers (2)

Fad
Fad

Reputation: 9858

You mean like this?

$qproducts = 0;
foreach ($this->cart->getProducts() as $product) {  
    $qproducts += $product['quantity'];
}       
$this->data['pquantity'] = $qproducts;

Upvotes: 5

Mark Byers
Mark Byers

Reputation: 838276

Use + (addition) instead of . (concatenation).

$qproducts = 0;
foreach ($this->cart->getProducts() as $product) {  
    $qproducts += $product['quantity'];
}   

Upvotes: 4

Related Questions