Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreach loop with multiple arrays

This is what I want:


foreach($_POST['something'] as $something){
    foreach($_POST['example'] as $example){
        $query = mysql_query("INSERT INTO table (row, row2) VALUES ('{$something}','{$example}')");
    }

}

$_POST['something'] and $_POST['example'] are arrays from an input with

name="something[]" and name="example[]".

The problem:


In this way I will send the data twice to database. So I need a solution where I can loop trough 2 arrays without seding the data twice.

EDIT

  • The two array will always have the same size
  • In the mysql_query I will have other elements not just row, row2, and those will be static without any array.
like image 609
Adam Halasz Avatar asked Dec 04 '22 11:12

Adam Halasz


1 Answers

Do you mean something like:

foreach($_POST['something'] as $key => $something) { 
    $example = $_POST['example'][$key];
    $query = mysql_query("INSERT INTO table (row, row2) VALUES ('{$something}','{$example}')"); 
} 
like image 163
Mark Baker Avatar answered Dec 21 '22 06:12

Mark Baker