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);"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?