-2

I am facing problem when I am trying to insert values from array to mysql database.

foreach ( $_POST['product_id'] as $key=>$value AND $_POST['discount'] as $key1=>$discount) { }

check the above given code where I am going wrong?

Bartek
  • 1,349
  • 7
  • 13

2 Answers2

1

You can use a regular for loop as long as the indexes match:

$count = count($_POST['product_id']);
for($i = 0; $i < $count; $i++) {
    echo $_POST['product_id'][$i];
    echo $_POST['discount'][$i];
}
Joseph
  • 5,070
  • 1
  • 25
  • 26
0

use array_map this will loop through all keys in all arrays provided simultaneously.

array_map(function(){
  $args = func_get_args();
  foreach($args as $k => $v) {
    echo $v;
  }
}, $arr1, $arr2 ...);
RaggaMuffin-420
  • 1,762
  • 1
  • 10
  • 14