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

  • Home
  • SEARCH
  • 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 9250457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:26:59+00:00 2026-06-18T10:26:59+00:00

I found a solution , see answer below and/or Github Gist, which has newer

  • 0

I found a solution, see answer below and/or Github Gist, which has newer optimizations.

I’ve got an array of credit card charges and a batch total… sometimes the SUM() of the amounts = the batch amount, so that’s easy to group to the batch and we can check off transactions contributing to that day’s deposit to the checking account.

Sometimes, the proper sum is for a subset of these charges, where 1 or more are batched the next day. Would you help me programmatically solve this? It’s for my authorize.net batch accounting, which sucks, so I’m making a tool for my bookkeeper.

+------------+--------+
| transId    | amount |
+------------+--------+
| 2863996511 | 154.00 |
| 2863992361 | 762.20 |
| 2863898975 |  49.00 |
| 2863894564 |   5.44 |
| 2863879671 |  10.88 |
| 2863858891 | 209.00 |
| 2863856334 | 148.00 |
| 2863367273 | -25.01 |
+------------+--------+

And the batch total for the day is $1302.63. As often happens, a charge didn’t end up in that batch, so the batch is some subset of the sum of charges in the array. In this case, the $10.88 charge was in the next day’s batch. This little bit of pseudocode can catch that via two nested for loops:

        for( $skipdx=0; $skipdx<$size; $skipdx++){
            $total=0;
            for( $idx=0; $idx<$size; $idx++){
                if($idx!=$skipdx){
                    $total+=$charges[$idx]['amount'];
                    $thisBatch[]=$charges[$idx]['transId'];
                }
                if( abs($total-$batch['total']) < .04 ) {
                    echo "sum of charges matches $date batch total: $$total  (line: ". __LINE__ .")\n";
                    $foundIt=TRUE;
                    break;
                }
            }
            if($foundIt==TRUE)
                break;
        }

How can I dynamically choose to search the charges where TWO are not added in? Then THREE? I can see that if $skipdx is one charge omitted, then two charges skipped would add a skip2dx nested loop. And if still not found, skip3dx would be 3rd level of nesting.

I’m usually really good at algorithms until recursion and then I go stupid.

  • 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-18T10:27:00+00:00Added an answer on June 18, 2026 at 10:27 am

    It came to me! The “present layer” of the recursion does this:

    1. Sum the charge amounts.
    2. Iterate through the array of charges,
      subtracting out 1 charge at a time and test against the target batch
      amount.
    3. If the amount matches in this iteration, unset the id of this charge and return the
      array of results.
    4. If not found, iterate again through the array of charges
      this time removing the current charge. call the function again with
      the same target amount and the new shortened array to the same
      function again.

    Working code (Github has newer optimizations, too):

    $charges=Array(
         '2863996511' => 154.00 ,'2863879671' => 10.88
        ,'2863992361' => 762.20 ,'2863858891' => 209.00
        ,'2863898975' => 49.00  ,'2863856334' => 148.00
        ,'2863894564' => 5.44   ,'2863367273' => -25.01
    ); print_r($charges);
    $targets=Array(1302.63, 1327.64, 1322.20 );
    foreach( $targets as $batch ) {
        printf( "We seek combination of transIds to = $%-7.2f\n", $batch);
        $answer=reco( $batch, $charges );
        if( is_array($answer) )
            echo eval( "return ".implode("+",$answer).";" )."\nWIN!\n";
        else
            echo "Bust!\n";
    }   
    
    function reco( $target, $arr ){
        if( count($arr) < 2 )
            return FALSE;
        $killid='';
        $sum = eval( "return ".implode("+",$arr).";" );
        foreach( $arr as $id=>$amt ){
            if( abs($sum-$target-$amt) < .01 ) {
                $killid=$id;
                break;
            }
        }
        if( strlen($killid) > 1 ) {
            echo ".  omit $killid : ".$arr[$killid]."\n";
            unset($arr[$killid]); 
            return $arr;
        }
        foreach( $arr as $id=>$amt ){
            $tmp=$arr;
            unset($tmp[$id]);
            $rtn = reco( $target, $tmp );
            if( is_array($rtn) ) {
                echo ".. omit $id : ".$arr[$id]."\n";
                return $rtn;
            }
        }
        return FALSE;
    }
    

    And the output:

    We seek combination of transIds to = $1302.63
    . omit 2863879671 : 10.88
    1302.63
    WIN!
    We seek combination of transIds to = $1327.64
    . omit 2863367273 : -25.01
    .. omit 2863879671 : 10.88
    1327.64
    WIN!
    We seek combination of transIds to = $1322.20
    . omit 2863367273 : -25.01
    .. omit 2863879671 : 10.88
    .. omit 2863894564 : 5.44
    1322.2
    WIN!
    

    Performance isn’t a great concern; to the third level of recursion processed in about a second.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE: I found a solution. See my Answer below. My Question How can I
I've found a solution, see my own answer below. Does anyone have a more
EDIT - FOUND A EASY 5-10 LINE SOLUTION!!! See MY OWN ANSWER BELOW!!! YAY!!!!!!!!!
SEE BELOW FOR SOLUTION BASED ON ANSWER/COMMENT from @Bertrand Le Roy ORIGINAL QUESTION: Not
Note, I have found the solution to my problem. Please see below. I'm converting
Update Solution Found See Bottom of post if interested Seems simple enough and for
Update: I've found a partial solution in this answer here , by adding the
I FOUND A SOLUTION FOR RESTORING MY VALUES, SEE MY NEW VERSION of populateFields()
Seems, it's common task, but I haven't found solution. I need to calculate number
Edited: SOLUTION FOUND. This is strange and not the best solution, but I just

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.