用PHP汇总数组值

时间:2013-06-10 19:28:51

标签: php loops

我有一组数组,其中包含以下数据;

此循环存储数据;

   while($stmntResult = mysql_fetch_assoc($stmnt))

    {          
         $userResult[] = $stmntResult["user"];
         $nameResult[] = $stmntResult["full_name"];
         $salescountResult[] = $stmntResult["sumcountSamnt"];
         $upResult[] = $stmntResult["sumcountupAmnt"];
         $countverifiedccResult[] = $stmntResult["sumccverifiedcountAmnt"];
         $amountverifiedccResult[] = $stmntResult["sumccverifiedAmnt"];
         $countverifiedbankResult[] = $stmntResult["sumbankverifiedcountAmnt"];
         $amountverifiedbankResult[] = $stmntResult["sumbankverifiedAmnt"];
         $damounttotalResult[] = $stmntResult["sumDamnt"];
         $upamounttotalResult[] = $stmntResult["sumUpamnt"];
         $mdamounttotalResult[] = $stmntResult["sumMdamnt"];
         $samounttotalResult[] = $stmntResult["sumSamnt"];
         $tmpUser[] = $stmntResult["user"];
         //echo $testResult[$z]. "-" . $nameResult[$z]."<br>";


    }
  $userid Array ( [0] => gdx [1] => hjx [2] => rnx [3] => tbr [4] => tdr [5] => tdy [6] => ter [7] => teu [8] => thr [9] => tht [10] => tje [11] => tjt [12] => toy [13] => tst [14] => ttu [15] => tvy [16] => twu [17] => twy [18] => txy [19] => tyu [20] => tze [21] => tzw [22] => gdx [23] => hjx [24] => rnx [25] => tbr [26] => tcx [27] => tdr [28] => tdy [29] => ter [30] => teu [31] => thr [32] => tht [33] => tje [34] => tjt [35] => toy [36] => tst [37] => ttu [38] => tvy [39] => twu [40] => twy [41] => txy [42] => tyu [43] => tze [44] => tzw [45] => gdx [46] => rnx [47] => tbr [48] => tdr [49] => tdy [50] => teu [51] => thr [52] => tje [53] => tjt [54] => toy [55] => tst [56] => ttu [57] => tvy [58] => twy [59] => tyu [60] => tze [61] => tzw )

 $sales Array ( [0] => 4 [1] => 6 [2] => 6 [3] => 4 [4] => 5 [5] => 1 [6] => 7 [7] => 2 [8] => 1 [9] => 4 [10] => 5 [11] => 9 [12] => 4 [13] => 2 [14] => 3 [15] => 2 [16] => 7 [17] => 2 [18] => 2 [19] => 6 [20] => 3 [21] => 6 [22] => 7 [23] => 5 [24] => 3 [25] => 1 [26] => 0 [27] => 6 [28] => 5 [29] => 4 [30] => 5 [31] => 3 [32] => 6 [33] => 7 [34] => 7 [35] => 5 [36] => 5 [37] => 7 [38] => 6 [39] => 3 [40] => 5 [41] => 3 [42] => 3 [43] => 5 [44] => 3 [45] => 6 [46] => 0 [47] => 5 [48] => 11 [49] => 0 [50] => 6 [51] => 0 [52] => 0 [53] => 8 [54] => 0 [55] => 7 [56] => 3 [57] => 0 [58] => 1 [59] => 2 [60] => 0 [61] => 7 ) 

每个数组在很多情况下循环多次3次,因此例如在userid数组中将有3个gdx实例我想显示每个用户的总数。目前我已将每个循环插入临时数据库

    for($i = 0; $i  < count($nameResult); ++$i)
  {

 $testinsert = "insert into stats2 (userid,
                fullname,
                salescountTotal,
                upamountcount,
                countvrfycc,
                amountvrfycc,
                countvrfybank,
                amountvrfybank,
                donationTotal,
                upsaleTotal,
                monthlysaleTotal,
                salesTotal,
                callCount,
                lastupdate)
          VALUES
               ('$userResult[$i]','$nameResult[$i]',
                 '$salescountResult[$i]',
                 '$upResult[$i]',
                 '$countverifiedccResult[$i]',
                 '$amountverifiedccResult[$i]',
                 '$countverifiedbankResult[$i]',
                 '$amountverifiedbankResult[$i]',
                 '$damounttotalResult[$i]',
                 '$upamounttotalResult[$i]',
                 '$mdamounttotalResult[$i]',
                 '$samounttotalResult[$i]',
                 '$callCount[$i]',


                 '$update')
          ";
    mysql_query($testinsert);

然后在重新查询时求和以获得

以下的输出
   Name | Sales
   gdx  |  9
   hjx  |  6

我想在php端进行数学运算以节省服务器的CPU时间,谢谢你的帮助。

每一行应该输出每个用户数据的总和,使用array_sum我可以得到所有用户的总数,但我希望得到每个用户总数而不必先插入临时数据库。

1 个答案:

答案 0 :(得分:1)

最简单的方法是迭代一个数组,根据键获取另一个数组的值。这就是我想出的:

<?php
$userid = array('gdx','hjx','gdx','hjx');
$sales = array(4,6,5,2);

$output = array();
foreach($userid as $k=>$v){
    if(!isset($output[$v])){
        $output[$v] = $sales[$k];
    }else{
        $output[$v] += $sales[$k];
    }
}

echo 'Name | Sales<br>';
foreach($output as $name=>$sales){
    echo $name.' | '.$sales.'<br>';
}