如何为每个循环使用两个数组存储备用值

时间:2019-03-18 06:24:20

标签: php mysql arrays mysqli foreach

我创建了两个数组,我想在数据库表中添加备用值

foreach($aaa[$key] as $key2 and $bbb[$key] as $key3) {
  $qd = "INSERT INTO aaa (xxx, yyy, status) VALUES ('$key2', '$key3', '1')";
  $dd = mysqli_query($conn, $qd);
  if ($dd) {
    echo '1';
  } else {
    echo mysqli_error($conn);
  }
}

1 个答案:

答案 0 :(得分:1)

因此,您希望数组有2个循环,这两个循环都将在查询中使用。您可以在循环中使用索引。

// count of items. from what you wanted to do to your loop
// it looks like it is given that the count of 2 arrays are
// the same.
$count = count($aaa[$key]);

for ($i = 0; $i < $count; $i++) {
    $qd = "INSERT INTO aaa (xxx, yyy, status) VALUES ($aaa[$key][$i], $bbb[$key][$i], '1')";
    $dd = mysqli_query($conn, $qd);

    if ($dd) {
        echo '1';
    } else {
        echo mysqli_error($conn);
    }
}