Add item with different array

Asked

Viewed 62 times

1

I have the following array()

Array
(
    [nome_responsavel] => 
    [telefone] => 
    [email] => 
    [dependentenome] => Array
        (
            [0] => qqq
            [1] => qqq
            [2] => qqqq
        )

    [dependentedata] => Array
        (
            [0] => 2017-01-18
            [1] => 2017-01-04
            [2] => 2017-01-06
        )

)

I need to make an Insert as follows

INSERT INTO tabela (nome, data) VALUES ('nome', 'data');

How can I do this, since I have an array with two different columns?

1 answer

2


That’s what you need?

$sql = 'INSERT INTO tabela (nome, data) VALUES ';
for($i = 0; $i < count($array['dependentenome']); $i++){
     $sqlValues[] = "('{$array['dependentenome'][$i]}', '{$array['dependentedata'][$i]}')";
}
$sql .= implode(', ', $sqlValues) . ';';

echo $sql;

Upshot:

INSERT INTO table (name, date) VALUES ('qqq', '2017-01-18'), ('qqqq', '2017-01-04'), ('qqqqq', '2017-01-06');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.