Как объединить три массива и вставить несколько строк - PullRequest
0 голосов
/ 05 марта 2020

У меня есть 3 массива, и я хочу, используя foreach l oop, добавить несколько строк в базу данных. Вот коды с я пытаюсь.

$array1 = ('Hello First row', 'Hello Second Row', 'Hello third row', 'Hello Forth row');
$array2 = ('This going to be First row', 'This going to be Second Row', 'This going to bethird row', 'This going to beForth row');
$array3 = ('also the first row', 'also the Second Row', 'also the third row', 'also the Forth row');
$stmt = $db->prepare("INSERT INTO table_name(first, second, third) VALUES (:first, :second, :third)");
$stmt->execute(array(':first' => $array1, ':second' => $array2, ':third' => $array3));

PS: я пытался использовать foreach l oop, прямо над функцией выполнения $ stmt, но это не сработало, как ожидалось.! Создано 12 строк в таблице. Которые должны быть только 4.

1 Ответ

0 голосов
/ 05 марта 2020
$array1 = ('Hello First row', 'Hello Second Row', 'Hello third row', 'Hello Forth row');
$array2 = ('This going to be First row', 'This going to be Second Row', 'This going to bethird row', 'This going to beForth row');
$array3 = ('also the first row', 'also the Second Row', 'also the third row', 'also the Forth row');
$stmt = $db->prepare("INSERT INTO table_name(first, second, third) VALUES (:first, :second, :third)");

for($index=0; $index<count($array1); $index++) {
    $stmt->execute(array(':first' => $array1[$index], ':second' => $array2[$index], ':third' => $array3[$index]));
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...