-1

i have a set of arrays that has the following data stored in in;

this loop stores the data;

   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 ) 

each array is looped multiple times in many cases 3 times, so for example in the userid array there will be 3 instances of gdx i want to display the totals for each user. Currently i have the each loop inserted into a temp database

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

and then summed upon re-query to get the output below

   Name | Sales
   gdx  |  9
   hjx  |  6

I was wanting to do the math on the php side to save CPU time of the server, thanks for your help.

Each line should output the sum total of each users data, using array_sum i can get the totals for all the users, but i want to get each users totals without having to insert into a temp database first.

Iz3k34l
  • 130
  • 2
  • 10
  • 6
    PHP runs on the server side, too. – Beat Jun 10 '13 at 19:30
  • I think you are looking for [`array_sum`](http://php.net/manual/en/function.array-sum.php). – Dave Chen Jun 10 '13 at 19:32
  • If the data is from javascript, you can use http://stackoverflow.com/a/3762623/2344142 – Dave Chen Jun 10 '13 at 19:33
  • What is the "total" ? the number of times a user appears in the first array ? the sum of the indexes ? why do you iterate each array a few times ? your question is not clear to me, please post some code and full examples, not arrays that end with `...` thanks. – Nir Alfasi Jun 10 '13 at 19:37

1 Answers1

1

The simplest way is to iterate through one array to get the other array's value based on the key. This is what I came up with:

<?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>';
}
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62