2

This code creates an array and adds attribute 'payment_sum' even if $value['CardCode'] != $prevCode is false. You can see that I'm printing the values for testing, the last line with 509.85 summing correctly, but the array before that should have been omitted according to this if statement.

     if (($handle = fopen('upload/BEN-new.csv', "r")) === FALSE) {
            die('Error opening file'); 
         }

         $headers = fgetcsv($handle, 1024, ',');
         $cardCodes = array();
         $payments = array();
         $details = array ();

        while ($row = fgetcsv($handle, 1024, ",")) {
               $cardCodes[] = array_combine($headers, $row);
        }

            $prevCode = '';

            foreach ($cardCodes as $key => $value) {

                if ($value['CardCode'] != $prevCode) {
                    $payments['payment_sum'] = $value['InvPayAmnt'];
                }
                else {
                    $payments['payment_sum'] += $value['InvPayAmnt'];
                }

                    $prevCode = $value['CardCode'];
                    print_r ($payments);
            }
            fclose($handle);

Printing...

   Array
     (
         [payment_sum] => 1055.40
     )

   Array
   (
         [payment_sum] => 550.00
   )

   Array
   (
         [payment_sum] => 100.00
    )

   Array
   (
        [payment_sum] => 287.33
   )
   Array
   (
       [payment_sum] => 509.85
   )

Preferred Output

   Array
     (
         [payment_sum] => 1055.40
     )

   Array
   (
         [payment_sum] => 550.00
   )

   Array
   (
         [payment_sum] => 100.00
    )

   Array
   (
       [payment_sum] => 509.85

         Array (
                  [currTotal] =>  287.33
                  [currTotal] =>  222.52
               )
   )

CSV

  BENV1072      1055.4
  BENV1073      550
  BENV5271      100
  BENV5635      287.33
  BENV5635      222.52
4
  • Why are you throwing out all the payments before BENV5635? For example, if you add BENV5627 23.54 to your input file, the "total" payments will be 23.54. Is that really what you want? Commented Jun 5, 2012 at 21:08
  • I think I'm not explaining well. Basically I am trying to group this array by the Vendor ID ie: BENxxxx and display the total amount for that group. As you see the last 2 in the CSV should be grouped together and only display the total of 509.85 Commented Jun 5, 2012 at 21:12
  • Yes, that's what I thought you would want, but that doesn't match your output. Commented Jun 5, 2012 at 21:13
  • @DavidHarkness thank you for taking the time to look at this, I modified the preferred result. That is ultimately what I need. Commented Jun 5, 2012 at 21:23

2 Answers 2

1

Each time through the for loop you either set $payments['payment_sum'] to the current row's payment ($value['InvPayAmnt']) or you add the row's payment to the total. Since the previous code is different from the current row's code for all but the final row, you're overwriting the running total over and over. Only the last row is added to the previous row's payment, giving 509.85 as the result.

You are treating $payments as a single-value variable rather than an array because the key under which you store the totals never changes. You need to use the vendor ID as the key.

$payments = array();
foreach ($cardCodes as $key => $value) {
    $payments[$value['CardCode']] += $value['InvPayAmnt'];
}
print_r($payments);

Output

Array
(
    [BENV1072] => 1055.4
    [BENV1073] => 550
    [BENV5271] => 100
    [BENV5635] => 509.85
)

You can easily have the $payments array track the individual payments from the file as well as the total.

$payments = array();
foreach ($cardCodes as $key => $value) {
    $payments[$value['CardCode']]['payments'][] = $value['InvPayAmnt'];
    $payments[$value['CardCode']]['total'] += $value['InvPayAmnt'];
}
print_r($payments);
Sign up to request clarification or add additional context in comments.

3 Comments

You are correct, because if there's only one payment - that is the total, if there are multiple than they need to be summed.
The above will give you one payment total per unique vendor code.
@user1413248 - When you assign a value using the array brackets, PHP will create any missing arrays or slots needed to hold it. In the second code snippet, $payments becomes a two-dimensional array--an array of arrays. Each subarray consists of a total mapped to the key total and an array containing individual payments mapped to the key payments. Move the print_r call inside the loop to see how the arrays form.
0

I would assume it's because both $value['CardCode'] and $prevCode are either undefined or equivalent. I don't see anywhere that you set $prevCode before you use it, even though it is in your if statement. At the top of your code, put error_reporting(-1); and you will likely be shown a notice.

2 Comments

Try adding ini_set('display_errors', 1); as well or check in your error log file.
My logic is correct here because if I do: if ($value['CardCode'] != $prevCode) { print $value['InvPayAmnt']; print '<br/>'; } It only omits the repeating values. But for some reason when I do this $payments['payment_sum'] = $value['InvPayAmnt']; it reprints both arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.