lennert_h
lennert_h

Reputation: 213

Count same fields from different arrays

I have this form, and the results are written to a csv file (inschrijvingen.csv) and it looks like this:

Person,City,Email,NumberOfP1,NumberOfP2,NumberOfP3,Total

which I then read out with

 <?php
 $rowcount = 0;
 $file = fopen('inschrijvingen.csv', "r");
 while (($artext = fgetcsv($file)) !== false) {
 $line = ++$rowcount;
 $naam = $artext[0];
 $city = $artext[1];
 $email = $artext[2];
 $adce = $artext[3];
 $adc = $artext[4];
 $che = $artext[5];
 $sum = $artext[6];
 print_r($artext);
 }
 fclose($file);

 ?>

This gives me:

Array ( [0] => Person1 [1] => City [2] => [email protected] [3] => 0 [4] => 1 [5] => 0 [6] => 15 ) 
Array ( [0] => Person2 [1] => City [2] => [email protected] [3] => 0 [4] => 3 [5] => 0 [6] => 45 ) 
Array ( [0] => Person3 [1] => City [2] => [email protected] [3] => 1 [4] => 2 [5] => 2 [6] => 71 ) 

That is good, it's correct, and I can display this all in a table. But now I would need the sum of every value in $artext[3] , $artext[4] , $artext[5] but since it's multiple arrays I can't really process them. I tried putting all the needed values in a different array of their own, but that didn't work.

So now I would like to know how I can count my values, so I have a total per product.

(In this example: $artext[3] sum = 1 , $artext[4] sum = 6 , $artext[5] sum = 2 )

To be clear: $sum = $artext[6]; contains the total price, as calculated by the form.

Upvotes: 0

Views: 31

Answers (1)

user1320635
user1320635

Reputation:

 <?php
 $rowcount = 0;

 $adceSum = 0;
 $adcSum = 0;
 $cheSum = 0;

 $file = fopen('inschrijvingen.csv', "r");
 while (($artext = fgetcsv($file)) !== false) {
 $line = ++$rowcount;
 $naam = $artext[0];
 $city = $artext[1];
 $email = $artext[2];
 $adce = $artext[3];
 $adc = $artext[4];
 $che = $artext[5];
 $sum = $artext[6];
 print_r($artext);

 $adceSum += $adce;
 $adcSum += $adc;
 $cheSum += $che;
 }

 // Here you have the sum in $adceSum, $adcSum and $cheSum.
 fclose($file);
 ?>

Upvotes: 1

Related Questions