This is the array that I get from a query:
Array (
[0] => Array (
[0] => Array (
[PRODUCT] => ROSE
[VARIETY] => ADELE
[GOLD] => 160
[NORMAL] => 0
[TOTAL] => 160
)
[1] => Array (
[PRODUCT] => ROSE
[VARIETY] => ALESSO
[GOLD] => 1320
[NORMAL] => 550
[TOTAL] => 1870
)
[2] => Array (
[PRODUCT] => ROSE
[VARIETY] => ANASTACIA
[GOLD] => 440
[NORMAL] => 150
[TOTAL] => 590
)
[3] => Array (
[PRODUCT] => ROSE1
[VARIETY] => ANASTACIA1
[GOLD] => 420
[NORMAL] => 120
[TOTAL] => 540
)
[4] => Array (
[PRODUCT] => ROSE1
[VARIETY] => ANASTACIA1
[GOLD] => 440
[NORMAL] => 100
[TOTAL] => 540
)
[5] => Array (
[PRODUCT] => ROSE2
[VARIETY] => ANASTACIA2
[GOLD] => 640
[NORMAL] => 0
[TOTAL] => 640
)
[6] => Array (
[PRODUCT] => ROSE2
[VARIETY] => ANASTACIA2
[GOLD] => 440
[NORMAL] => 440
[TOTAL] => 880
)
)
)
)
The keys GOLD
and NORMAL
can be differents, depend of the query, but I would like to sum the total from GOLD
, NORMAL
and TOTAL
by PRODUCT
, something like this, you can omit VARIETY
:
Array(
[0] => Array(
[PRODUCT] => ROSE
[GOLD] => 1920
[NORMAL] => 700
[TOTAL] => 2620
)
[1] => Array(
[PRODUCT] => ROSE1
[GOLD] => 860
[NORMAL] => 220
[TOTAL] => 1080
)
[2] => Array(
[PRODUCT] => ROSE2
[GOLD] => 1080
[NORMAL] => 440
[TOTAL] => 1520
)
)
I have tried somenthing like this:
$harvest
is the array with the data
//This get the array_keys from the data
$arrayThead = array();
for ($i=0; $i < count($harvest) ; $i++) {
array_push($arrayThead, array_keys($harvest[$i][0]));
}
$arrayfoot= array();
foreach ($harvest as $key => $value) {
foreach ($value as $harv) {
foreach ($arrayThead as $key => $values) {
foreach ($values as $th) {
if($th != 'PRODUCT' && $th != 'VARIETY'){
$arrayfoot[$th] += $harv[$th];
}
}
}
}
}
But at this point it totalizes the data with all the PRODUCTS in general:
Array (
[GOLD] => 3850
[NORMAL] => 1360
[TOTAL] => 5220
)
UPDATED
This is the mysql query:
SELECT pr_products.product AS PRODUCT,
pr_varieties.variety AS VARIETY,
FORMAT(SUM(IF(pr_grades.grade='GOLD',pf_harvest.quantity,0)),0) AS GOLD,
SUM(IF(pr_grades.grade='NORMAL',pf_harvest.quantity,0)) AS NORMAL,
SUM(pf_harvest.quantity) AS TOTAL
FROM pf_harvest
INNER JOIN pf_performance ON pf_performance.id = pf_harvest.id_performance
INNER JOIN pr_products ON pr_products.id = pf_harvest.id_product
INNER JOIN pr_varieties ON pr_varieties.id = pf_harvest.id_variety
INNER JOIN pr_grades ON pr_grades.id = pf_harvest.id_grade
WHERE pf_performance.status = 100
AND pf_harvest.id_tenant = 1
AND pf_harvest.date = CURDATE()
GROUP BY pf_harvest.id_product, pf_harvest.id_variety
ORDER BY pf_harvest.id_product, pr_varieties.variety, pf_harvest.id_grade
How can I add the data to arm an array as in the example I have explained? Thanks!