几种组合的PHP总和循环结果

First of all many thanks for all good answers Stackoverflow community provides. I have always managed to find the answers here. This time seems a bit different because after a few days searching I can not find an answer . Probably because I do not know how to correctly ask the question for this specific case !?

Case :

PHP Loop result from a mysql table :

          array {
               [0]=>
                   ["productName"]=> P1
                   ["qty"]=> 1
                   ["color"]=> black
                }
               [1]=>
                   ["productName"]=> P1
                   ["qty"]=> 1
                   ["color"]=> red
                }
               [2]=>
                   ["productName"]=> P2
                   ["qty"]=> 1
                   ["color"]=> blue
                 }
               [3]=>
                   ["productName"]=> P3
                   ["qty"]=> 1
                   ["color"]=> black
                 }
               [4]=>
                   ["productName"]=> P3
                   ["qty"]=> 1
                   ["color"]=> red
                 }
               [5]=>
                   ["productName"]=> P2
                   ["qty"]=> 1
                   ["color"]=> green
                }
             .....more and more.....
            }
  • Products P1,P2,P3,P4 can be in black,red,green or blue.
  • We have all possible combinations here below :

        P1 - black, P1 - red, P1 - green, P1 - blue.
    
        P2 - black, P2 - red, P2 - green, P2 - blue.
    
        P3 - black, P3 - red, P3 - green, P3 - blue.
    
        P4 - black, P4 - red, P4 - green, P4 - blue.
    

QUESTION : How to get the total sum quantities of each combination ?

We can get the results by using below code but of course you have to write the same code for every single combination. It does not make sense....Imagine 100 products or more....

$sumP1Black = 0;

foreach ($dao->getArticles() as $value) {

        if(($value->productName == 'P1') && ($value->color == 'black')) {       
        $total = $value->qty;
        $sumP1Black += $total;
        }   
}

print_r($sumP1Black);   

I would like to know how can we correctly code for this specific case. I did not include it in this example but you could also add to above example size of the products (XL,L,M,...)...

It would be great If someone could "push me" in the right direction...

Many thanks for your time,

Ric

Try this hope it will help you out. In this example i took a big array with multiple values which will be aggregated on product and its color,Using my code if you want to retrieve value of quantity of P1 of black then just echo $result["P1,black"]["qty"];

PHP code demo

<?php
$array=array (
               0 => array(
                   "productName"=> "P1",
                   "qty"=> 1,
                   "color"=> "black"
                ),
               9 => array(
                   "productName"=> "P1",
                   "qty"=> 1,
                   "color"=> "black"
                ),
               1 => array(
                   "productName"=> "P1",
                   "qty"=> 1,
                   "color"=> "red"
                ),
               11 => array(
                   "productName"=> "P1",
                   "qty"=> 8,
                   "color"=> "red"
                ),
               2 => array(
                   "productName"=> "P2",
                   "qty"=> 1,
                   "color"=> "blue",
                 ),
               3 => array(
                   "productName"=> "P3",
                   "qty"=> 1,
                   "color"=> "black",
                 ),
               4 => array(
                   "productName"=> "P3",
                   "qty"=> 1,
                   "color"=> "red",
                 ),
               5 => array(
                   "productName"=> "P2",
                   "qty"=> 1,
                   "color"=> "green",
                ),
               6 => array(
                   "productName"=> "P2",
                   "qty"=> 1,
                   "color"=> "green",
                )
);
$result=array();
foreach($array as $key => $value)
{
    $productName=$value["productName"];
    $color=$value["color"];
    $key=$productName.",".$color;//creating key combinations to make data accessible with given specified requirement
    if(isset($result[$key]))
    {
        $result[$key]["qty"]+=$value["qty"];
    }
    else
    {
        $result[$key]=$value;
    }
}
print_r($result);

Thanks Sahil for your helpful answer.

I just added the size to his code :

foreach($dao->getArticles() as $value) {
   $productName = $value->productName;
   $color = $value->color;
   $size = $value->size;
   $key = $productName" - ".$color." - ".$size; //creating key combinations to make data accessible with given specified requirement

   if(isset($result[$key])) {
       $result[$key]->quantite += $value->quantite; 
      }else{
    $result[$key]= $value;
    }
}
print_r($result);

The results can be printed in the screen like: combination - Qty

P1 - Black - XL -> Qty

P1 - Black - L -> Qty

...... more and more.....

For output purposes a table like below might be better option for users :

          Qties     XL  L   M   S   Total 
        P1 - Black  5   2   2   1   10
        P1 - Green  4   1   3   2   10
         P1 - Red   1   2   2   3   8
       P1 - Blue    2   5   3   6   16
     P2 - Black     1   4   2   7   14
     P2 - Green     3   3   2   1   9
        P2 - Red    1   6   1   2   10
      P2 - Blue     2   7   1   8   18
     P3 - Black     1   4   1   2   8
     P3 - Green     2   3   1   4   10
        P3 - Red    4   4   1   1   10
      P3 - Blue     6   2   4   2   14
     P4 - Black     7   3   2   5   17
     P4 - Green     8   2   9   2   21
        P4 - Red    9   2   1   1   13
       P4 - Blue    2   1   2   1   6
       Total        58  51  37  48  194

I try to figure out what is the simplest way to get the above table result...