2

I'm working on a shipping calculator and i have a problem,let me tell you what i have and then i'll tell you my problem,

My array and foreach:

$array = unserialize($_SESSION['__vm']['vmcart']); 
foreach($array->products as $product){

$price = $product->product_price;
$amount = $product->quantity;
$length = $product->product_length;
$width = $product->product_width;
$height = $product->product_height;
$weight = $product->product_weight;
$supplier = $product->product_unit; 

 }

and everything works with the above code (That's not my problem)

My problem is:

the calculator has to take all the weight from supplier 1 and add them together and then do the same for supplier 2 (see below)

Example:

Product1 - weight is 5kg and it's from supplier 1

Product2 - weight is 2kg and it's from supplier 1

Product3 - weight is 9kg and it's from supplier 2

supplier 1's weight = to 7kg

supplier 2's weight = to 9kg

The same thing if it's the other way around,

Now what i have tried is a if statement (see below) but all it's doing is adding every products weight together,

if ($supplier =='S1'){
    $s1_total_weight += $product->product_weight;
} if ($supplier =='S2'){
    $s2_total_weight += $product->product_weight;
}

echo 'Supplier 1 '.$s1_total_weight.'<br>';
echo 'Supplier 2 '.$s2_total_weight.'<br>';

i use the below code to get the supplier number (S1 = supplier 1 - S2 = supplier 2),

$supplier = $product->product_unit;

I'm new to php but i think it has to do with the below code or am i wrong? can it be the if statement?

$product->product_weight

Please let me know if you need any more information,

if you have a better title for this topic please change it :)

Thanks for any help.

1 Answers1

2
$weights = array();
foreach ($array->products as $product) {
    if (isset($weights[$product->product_unit])) {
        // there was already some weight for this supplier, just add to it
        $weights[$product->product_unit] += $product->product_weight;
    }
    else {
        // first time we encounter this supplier, set weight
        $weights[$product->product_unit] = $product->product_weight;
    }
}

foreach ($weights as $supplier => $totalWeight) {
    echo "Total weight for supplier {$supplier} is {$totalWeight}.";
}
Tomas Creemers
  • 2,645
  • 14
  • 15
  • Hi i'm getting a weird output - if i only echo out the $totalWeight i get "Array1001100ArrayArrayArray15315310" if i add the $supplier to the echo it just add the $key value - any ideas? – MasterGray101 Aug 15 '13 at 13:35
  • Is it possible that your $array->products does not always contain a number for the 'product_weight' property? If it is not always a number (but for example sometimes an array), assigning them to the `$weights` array could give these results. Try writing it out to verify. (`foreach ($array->products as $product) { var_dump($product->product_weight); }`) – Tomas Creemers Aug 15 '13 at 14:49
  • Hi Tomas if i only use my array and the above foreach you gave i get all the products weight "string '4.8000' (length=6) string '5.2000' (length=6) string '5.0000' (length=6)" as you can see - what do you think is the problem? - i will still try some thing and see if i can get a result. – MasterGray101 Aug 15 '13 at 18:36
  • Tomas the problem was when i tested your code i did not add the $weights = array(); at the begging i replaced it with my array - everything is working - Thank you so much man - have a great day!! – MasterGray101 Aug 15 '13 at 18:55