Save duplicate arrays in an Insert

1

Have duplicate arrays

produto[] valor[] qtd[] total[]
produto[] valor[] qtd[] total[]

I need to save these arrays in an insert, like this:

INSERT 'tabela' (produto, valor, qtd, total) VALUES (produto[],valor[],qtd[],total[]) ;

INSERT 'tabela' (produto, valor, qtd, total) VALUES (produto[],valor[],qtd[],total[]) ;

// NOTE: I know this in VALUES does not work but that's what I need ...

ALREADY DONE:

    foreach ( $produto as $key => $v) {
    $array[$key][] = $v;
}

    foreach ( $valor as $key => $v) {
    $array[$key][] = $v;
}

    foreach ( $qtd as $key => $v) {
    $array[$key][] = $v;
}

    foreach ( $total as $key => $v) {
    $array[$key][] = $v;
}

    foreach ( $modalidade as $key => $v) {
    $array[$key][] = $v;
}

    foreach( $array as $value){ 

echo "INSERT INTO 'table' ( 'produto', 'valor','qtd','total','modalidade') VALUES (".$value[0].",".$value[1].",".$value[2].",".$value[3].",".$value[4].")

}

It's working, the fact is that not always in value [0] is the product, and value [1] is value and so on ....

    
asked by anonymous 05.01.2017 / 03:27

1 answer

2

You can do something like this:

$i=0;
$sql= "INSERT 'tabela' (produto, valor, qtd, total) VALUES ";
while(!empty(produto[$i]){
 $sql=$sql."($produto[$i],$valor[$i],$qtd[$i],$total[$i]),";
$i++;
 }
 $sqlFinal = substr($sql, 0, strlen($sql)-1);

Or another way would be this:

$i=0;
$sql= "INSERT 'tabela' (produto, valor, qtd, total) VALUES ";
foreach($produto as $p){
$sql=$sql."($p,$valor[$i],$qtd[$i],$total[$i]),";
$i++;
}
 $sqlFinal = substr($sql, 0, strlen($sql)-1);

Then you will have sql mounted in the $ sql variable

    
05.01.2017 / 04:00