在php中处理多种类型的数组


handling multiple types of arrays in php

我有多个动态文本框。

<input type="text" name="Paid_Amount[]">
<input type="text" name="Charges_Type[]">

每个文本框值在数据库中都有多行(分期付款)。支付的金额应根据分期付款的次数进行划分。例如,我有开发费用=50,000如果开发向收费,则分期付款

分期付款表---------------------------------------------------------------ID Charges_Type Installment_Amount Paid_Amout---------------------------------------------------------------1土地成本100002土地成本100003土地成本100004土地成本100005土地成本100006开发费用50007开发费用5000

我需要将已付款金额(从<form>获得)设置为分期付款表的已付款金额

要求是:如果我在土地成本中设置12,000,那么第一期应设置10000,剩余的2000将在下一期中设置

我尝试过foreachdo while循环,但对多个数组感到困惑:

$Charges_Type = $this->input->post('Charges_Type'); // array
$Paid_Amount = $this->input->post('Paid_Amount'); // array
$result = array of rows from installment table

如何处理这些3 arrays:

$merge = array_merge($Paid_Amount, $result);
    foreach ($merge as $key => $value)
                    {
                        if(!empty($merge[$key]))
                        {
                            foreach($result as $in)
                            {
                                if($in['Charges_Type'] == $key)
                                {
                                    $i = 0;
                                    do 
                                    {
                                        if($in['Installment_Amount'] >= $value && $in['Paid_Amount'] == 0 && $in['Charges_Type'] == $key)
                                        {
                                            echo "UPDATE `tbl_installment` SET `Paid_Amount` = '".$value."', `Paid_Date` = '".$Paid_Date."', `Is_Paid` = '1' WHERE `ID` = '".$in['ID']." AND `Charges_Type` = '".$key."''";
                                            $value = $value - $in['Installment_Amount'];
                                        }
                                        if($in['Installment_Amount'] < $value && $in['Charges_Type'] == $key)
                                        {
                                            echo "UPDATE `tbl_installment` SET `Paid_Amount` = '".$in['Installment_Amount']."', `Paid_Date` = '".$Paid_Date."', `Is_Paid` = '1' WHERE `ID` = '".$in['ID']." AND `Charges_Type` = '".$key."''";
                                            $value = $value - $in['Installment_Amount'];                        
                                        }
                                        if($i['Paid_Amount'] != 0 && $in['Charges_Type'] == $key)
                                        {
                                            $value = $value + $i['Paid_Amount'];
                                            echo "UPDATE `tbl_installment` SET `Paid_Amount` = '".$value."', `Paid_Date` = '".$Paid_Date."', `Is_Paid` = '1' WHERE `ID` = '".$in['ID']." '";
                                            $value = 0;
                                        }   
                                        $i++;
                                    } 
                                    while ($value > 0);
                                }
                            }
                        }       
                    }

我还没有回答您的问题,但提供了一种编写代码的替代方法,通过重构代码使其更容易阅读。这使处理多个数组循环时更容易。

我注意到在你的分期付款表中,你有一个拼写错误,Paid_Amout,我不确定这是你发布时的拼写错误,还是你的SQL中的错误。但这可能会引发一些问题。

$merge = array_merge($Paid_Amount, $result);
foreach ($merge as $key => $value)
{
    if(empty($merge[$key]))
        continue;
    foreach($result as $in)
    {
        if($in['Charges_Type'] != $key)
            continue;
        $i = 0;
        do 
        {
            if($in['Installment_Amount'] >= $value && $in['Paid_Amount'] == 0 && $in['Charges_Type'] == $key)
            {
                echo "UPDATE `tbl_installment` SET `Paid_Amount` = '".$value."', `Paid_Date` = '".$Paid_Date."', `Is_Paid` = '1' WHERE `ID` = '".$in['ID']." AND `Charges_Type` = '".$key."''";
                $value = $value - $in['Installment_Amount'];
            }
            if($in['Installment_Amount'] < $value && $in['Charges_Type'] == $key)
            {
                echo "UPDATE `tbl_installment` SET `Paid_Amount` = '".$in['Installment_Amount']."', `Paid_Date` = '".$Paid_Date."', `Is_Paid` = '1' WHERE `ID` = '".$in['ID']." AND `Charges_Type` = '".$key."''";
                $value = $value - $in['Installment_Amount'];                        
            }
            if($i['Paid_Amount'] != 0 && $in['Charges_Type'] == $key)
            {
                $value = $value + $i['Paid_Amount'];
                echo "UPDATE `tbl_installment` SET `Paid_Amount` = '".$value."', `Paid_Date` = '".$Paid_Date."', `Is_Paid` = '1' WHERE `ID` = '".$in['ID']." '";
                $value = 0;
            }   
            $i++;
        } while ($value > 0);
    }
}