doudi2005 2015-10-05 02:34
浏览 19
已采纳

在另一个数组中插入数组

I wrote a code which will get the monthly amortization of customer and the balance of customer's total payment excluding the customer monthly amortization. My problem is how do I insert the result into another array?

    $remaining = 12200;
    $amort = 5742;

    for($remaining += $amort; 

    $remaining > $amort; 

    ($result[] = ($remaining-=$amort) < $amort ? $remaining : $amort)

    );

print_r($result);

Here's the result

Array ( [0] => 5742 [1] => 5742 [2] => 716 ) 

And, I want to insert the $result array into another group of array, Here's my code.

$data4[] = array('collection_payment'=>$result);

the result of $data4 will be the one to insert in database mysql, so if you print the $data4, this will be the result,

Array ( [collection_payment] => 5742 [collection_payment] => 5742 [collection_payment] => 716 ) 

Any ideas?

  • 写回答

1条回答 默认 最新

  • douxuzui4590 2015-10-05 02:55
    关注

    It's still weid.

    I'd like to change your code but you'll get the idea once you read it all:

    $remaining = 12200;
    $amort = 5742;
    $rest = $remaining % $amort;
    $qt = (12200 - $rest) / $amort;
    $text = "";
    for($i=0;$i<$qt;$i++){
        $text .= ($i>0) ? ",($amort)" : "($amort)";
    }
    if($rest > 0){
        $text .= ",($rest)";
    }
    


    So if you do:

    "INSERT INTO table_name (COLUMNs) VALUES $text;";
    

    this will show up:

    "INSERT INTO table_name (COLUMNs) VALUES (5742),(5742),(716);";
    

    and insert all the values at once. If you have more than one column, you can adapt the code to add the values at each loop of the FOR and add the names on the insert so the result is this:

    "INSERT INTO table_name (column_name1, column_name2) VALUES (5742,val1),(5742, val2),(716, val3);"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序