首页 > 解决方案 > 如何在 foreach 循环的每个迭代中更改 post 值

问题描述

我需要在 foreach 循环的每次迭代中更改 post 值。这是我的代码:

foreach ($amovess as $key => $val) {
    $actions = $amovess[$key];
    $lastid = $database->insert("tablename", [
        "Action" => $actions,
        "barcode"=>$data['barcode1txt'],
        "barcode"=>$data['barcode2txt']
    ]);
}

我希望 foreach 循环的第一次移动$data['barcode1txt']应该插入,第二次移动$data['barcode2txt']应该插入。请指导我。

标签: phpmysql

解决方案


我不知道你的关键价值是什么。你可以这样做

<?php

    $i =1;
    foreach ($amovess as $key => $val) { 
        $actions = $amovess[$key];
        $lastid = $database->insert("tablename", [
                    "Action" => $actions,
                    "barcode" => $data['barcode' . $i . 'txt']
                ]);
        $i++;
    }

?>


推荐阅读