What are the best practices for inserting arrays into a database table using PDO in PHP?

When inserting arrays into a database table using PDO in PHP, it is important to use prepared statements to prevent SQL injection attacks. One way to achieve this is by looping through the array and binding each value to a parameter in the prepared statement before executing it. This ensures that the data is properly sanitized and inserted into the database table.

// Assume $pdo is your PDO connection object and $data is the array to be inserted

// Prepare the SQL statement with placeholders for each column
$columns = implode(', ', array_keys($data));
$values = ':' . implode(', :', array_keys($data));
$sql = "INSERT INTO your_table ($columns) VALUES ($values)";

// Prepare the statement
$stmt = $pdo->prepare($sql);

// Bind each value from the array to its corresponding placeholder
foreach ($data as $key => $value) {
    $stmt->bindValue(':' . $key, $value);
}

// Execute the statement
$stmt->execute();