How to change post value in each iterarion of foreach loop

Viewed 80

I need to change the post value on each iteration of foreach loop. Here is my code:

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

I want that on first move of foreach loop $data['barcode1txt'] should insert and on second move $data['barcode2txt'] should insert. Please guide me.

1 Answers

I dont know what is your key value coming . you can do like this

<?php

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

?>

Related