What is the best practice for inserting an array of values into a database using PHP?

When inserting an array of values into a database using PHP, the best practice is to use prepared statements to prevent SQL injection attacks and improve performance. This involves preparing the SQL query with placeholders for the values, binding the values to the placeholders, and executing the query.

// Sample array of values to insert
$data = [
    ['John', 'Doe', 'john.doe@example.com'],
    ['Jane', 'Smith', 'jane.smith@example.com'],
    ['Mike', 'Johnson', 'mike.johnson@example.com']
];

// Prepare the SQL query
$stmt = $pdo->prepare("INSERT INTO users (first_name, last_name, email) VALUES (?, ?, ?)");

// Iterate through the array and execute the query for each set of values
foreach ($data as $row) {
    $stmt->execute($row);
}