I have two arrays
$invoice = array
(
array("id"=>1,"amt"=>2000.00),
array("id"=>2,"amt"=>3000.00),
array("id"=>3,"amt"=>4000.00)
);
$payments = array
(
array("id"=>11111,"amt"=>500.00),
array("id"=>22222,"amt"=>3000.00),
array("id"=>33333,"amt"=>4500.00),
array("id"=>44444,"amt"=>1500.00)
);
my intention is to settle invoice "amt" from payment "amt" the script will execute in an orderly fashion.
However, the size of the two array elements may vary.
for example: 1. invoice array's first element "amt" 2000 will be deducted by payment array's first element "amt" 500
after the deduction the balance 1500 of the invoice value will be deducted from the second element of payments array "amt" which is 3000
this will proceed until i reach the last payment array element
The code below is what i have written and so far this does one successful iteration but fails to do the same on the second iteration as the nested foreach loop starts from the begining.
$flag = 0; //default / initial value 0 - start / 1- invoice / 2 - payments
$setlData = 0; // remaining balance after a settlement
$invoice_count = count($invoice);
$trx_count = count($payments);
$iteration = 0;
$output = "<table><thead><tr><th>inv#</th><th>invoice value</th><th>txn#</th><th>payment value</th><th>balance after settlement</th><th>from</th></tr></thead><tbody>";
foreach($payments as $p){
//echo($flag."<br />");
if($setlData > 0 and $flag == 1) {
if($i["amt"] < $p["amt"]){
$setlData = $p["amt"] - $setlData;
$output .= "<tr>";
$output .= "<td>".$i["id"]."</td><td>".$setlData."</td><td>" . $p["id"] . "</td><td>". $p["amt"]."</td><td>".$setlData."</td><td>Trx</td>";
$output .= "</tr>";
$flag = 2;
break;
}
}
foreach($invoice as $i){
if($setlData <= 0 and $flag == 0){
if($i["amt"] > $p["amt"]){
$setlData = $i["amt"] - $p["amt"];
$output .= "<tr>";
$output .= "<td>".$i["id"]."</td><td>".$i["amt"]."</td><td>" . $p["id"] . "</td><td>". $p["amt"]."</td><td>".$setlData."</td><td>invoice</td>";
$output .= "</tr>";
$flag = 1;
break;
}
}
}
}
$output .= "</tbody></table>";
echo($output);
I did search allover online, google and stackoverflow and i am unable to figure out how to pass on the value to the next array element of the nested loop.
Please advice.