Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:39:32+00:00 2026-06-05T06:39:32+00:00

This code creates an array and adds attribute ‘payment_sum’ even if $value[‘CardCode’] != $prevCode

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:39:34+00:00Added an answer on June 5, 2026 at 6:39 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code, which creates an image, and then adds some effects to
How can I write this code more cleanly/concisely? /// <summary> /// Creates a set
I have an array created with this code: var widthRange = new Array(); widthRange[46]
This is my code to create a file. public void writeToFile(byte[] array) { try
This is the code in my view: echo $this->Form->create('Chart'); echo $this->Form->input('username', array('label'=>('Usernames List'), 'empty'=>('Select
I have a code like <span ><img src="xxx.png">Tab1</span><span ><img src="yyy.png">Tab2</span> This code creates two
Well ive got this code that creates a table from JSON data. If a
I have this JavaScript code that creates a table, and puts in the rows
this code works perfectly. Creates an XDocument, puts it in cache, and retrieves it
I have this function in a Code Igniter model that creates a new video.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.