Algorithm to find the optimal payment schedule : I've got an array of bank accounts that have a minimum monthly payment requirement and I'm trying to figure out the logic to get the optimal payment schedule. Here's the array structure: 

    array(4) {
      ["account_one"]=>
      array(9) {
        ["account"]=>
        string(21) "Account One"
        ["balance"]=>
        int(2500)
        ["minimum_payment"]=>
        int(1000)
      }
      ["account_two"]=>
      array(9) {
        ["account"]=>
        string(16) "Account Two"
        ["balance"]=>
        int(1500)
        ["minimum_payment"]=>
        int(500)
      }
      ["account_three"]=>
      array(9) {
        ["account"]=>
        string(10) "Account Three"
        ["balance"]=>
        int(3000)
        ["minimum_payment"]=>
        int(750)
      }
      ["account_four"]=>
      array(9) {
        ["account"]=>
        string(14) "Account Four"
        ["balance"]=>
        int(3000)
        ["minimum_payment"]=>
        int(750)
      }
    }

In this example, the total amount of money in the accounts is 10000 and it needs to be moved between each account to meet the minimum payment requirement. The payment schedule also needs to be as efficient as possible, with the fewest number of payments to meet the requirements. This is a simple example, but in production we might have 15 or more accounts with varying minimum payment amounts.

What I've got so far isn't very much as I'm struggling to grasp the overall logic I need:

    $accounts_by_amount (array of accounts sorted by balance high to low)
    $accounts_reversed ($accounts_by_amount in reverse order)
    $schedule = array()
    foreach $accounts_by_amount as $account_a
        foreach $accounts_reversed as $account_b
            if $account_a['minimum_payment'] > $account_b['mininum_payment']
                // use higher minimum payment to reduce total payments needed
                $schedule_b[] = array(
                  'from' => $account_a['account'],
                  'to' => $account_b['account'],
                  'amount' => $account_a['minimum_payment'],
                );
            else
                continue;
    
        if $schedule_b
            $schedule += $schedule_b
        else
            continue

This doesn't work, obviously, but I think I might be making this overly complex with the loop within a loop. I'm not looking for somebody to actually write code for me, more to tell me how dumb I'm being with the logic and a rough idea of how to pull this schedule together with the minimum number of payments.

Any ideas greatly appreciated/please don't downvote :P