php - How to Make to Inner foreach loop load faster?

Good day.

Just to ask I have 2 foreach loop; goes like this (see below); my inner loop has more than 20,000 data which the outer loop matches it 20k times to check if the data matches at the outer loop; I have outerloop that holds the data for persons which get to almost 10k as of the moment. (which is growing).

foreach ($resultsPerson as $key => $valouter) {/*outer loop*/
   $outerclientno = $valouter['client_no'];
   $person_name = $valouter['person_name'];
   foreach ($resultsCashPaid as $key => $valinner) {/*inner loop*/
    $innerclientno = $valinner['client_no'];
    if($outerclientno==$innerclientno){
       $amount_per_person += $valinner['amount_paid_at_date'];
       $mode_of_payment = $valinner['mode_of_payment']; 
          if($mode_of_payment == 1 ) {`monthly`
              $creditamount      = $amount_per_person/1.08;
          }else if($mode_of_payment == 2 ) {`quarterly`
            $creditamount    = $amount_per_person/1.06;
          }else if($mode_of_payment == 3 ) {`semi-annual`/
            $creditamount    = $amount_per_person/1.04;
          }else if($mode_of_payment == 4 ) {`annual`
            $creditamount    = $amount_per_person/1;
          }
    }
}
echo "NAME: ". $person_name . " Total Cash: " . $amount_per_person;

}

The problem is that it takes almost 3 minutes to load the data. because of the inner loop of 20kplus of data. *i was imagining it loop 10,000 x 20,000. :O i don't know what else is best.

The reason i need to check every amount is because I need to verify first the mode of payment the amount is paid at that time its paid. *Mode of payment (monthly, quarterly, semi-annual, annual). So i need to check of what mode is the payment too.

Any advise from your. Any alternatives of loop? Thank you so much for any help.

Answer

Solution:

Yes, Zoli. Data is coming from a database, i think i have no choice but to really update my database to workout my data to export. It's a bit quite hard to explain here but its really on my database that should be updated. Because the output in my report should look like this.

Example:
(Paid last January mode of monthly)

AMOUNT PAID - Start Balance - END BALANCE

500 - 30000 - 29500

(The nxt month FEbruary (he/she) changes here mode to Annually)

  • let say his annual payment is 6000 so;

AMOUNT PAID Start Balance END BALANCE

  6000        29500        23500

Thank you all for trying to help. Keep safe.

Source