dongyao1895 2018-11-06 13:17
浏览 30

PHP:嵌套的foreach循环和传递值

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

  1. after the deduction the balance 1500 of the invoice value will be deducted from the second element of payments array "amt" which is 3000

  2. 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.

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c