php - 通过参数 php 对数组中的数据求和

标签 php mysql arrays

这是我从查询中获得的数组:

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 
                                   )
                    )


     ) 
)

GOLDNORMAL可以不同,具体取决于查询,但我想对GOLDNORMALTOTAL by PRODUCT,类似这样,您可以省略 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
                  )
)

我尝试过这样的事情: $harvest 是包含数据的数组

//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];
           }
        }
     }
  }
}

但此时它会汇总所有产品的数据:

Array ( 
        [GOLD] => 3850 
        [NORMAL] => 1360 
        [TOTAL] => 5220 
      )

已更新

这是 mysql 查询:

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

如何像我解释的示例中那样将数据添加到数组中? 谢谢!

最佳答案

    <?php
    $array = 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,
            )
        )
    );


    $totalByProduct = array();

    foreach ($array as $items) {
        foreach ($items as $item) {
            if(!key_exists($item['PRODUCT'], $totalByProduct)){
                $totalByProduct[$item['PRODUCT']] = $item;
                continue;
            }
            $totalByProduct[$item['PRODUCT']]['GOLD'] += $item['GOLD'];
            $totalByProduct[$item['PRODUCT']]['NORMAL'] += $item['NORMAL'];
            $totalByProduct[$item['PRODUCT']]['TOTAL'] += $item['TOTAL'];
        }
    }
    var_dump($totalByProduct);
?>

关于php - 通过参数 php 对数组中的数据求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43143337/

相关文章:

php - 在 PHP 中自动从 URL 下载文件?

jQuery比较并找出2个json数组之间的差异

php - 使用 JSON 从 MYSql 获取数据到我的 iOS 应用程序

MySQL:如何存储高度/体重信息?

c - 如何只检查一次输入,而不是挂起等待输入

java - 对 URL 的内容进行压缩 - Java

php - 向MYSQL中插入字符串和数组的组合

由于身份验证错误,Php Soap 请求失败

php - 无法浏览 mysql 查询结果

php - 选择行 - 还剩 24 小时